wood burning stoves
The moose likes Beginning Java and the fly likes Lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Lists" Watch "Lists" New topic


Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
I need a list to store pairs of short strings with the ability to use either string to return the other (kind of like a map). Say the stings are an IP address and the other a computer name. I'd like to be able to do something like:
String name = getName(ipAddr); and
Sting ipAddr = getIP(name);

I'd also like to be able traverse the entries to get each ipAddress;

Is there a java list that has this capability or should I roll my own? Right now, for testing, I'm using HashTable. I think that accessing the list should be thread safe as well - but I've not learned enough about that yet.

My list will be small, probably less than half a dozen entries.

Bert Wilkinson
Ranch Hand

Joined: Oct 28, 2009
Posts: 33
I don't think there is a Java Class that supports a key - key relationship like you want. You could force this, as you mentioned using the methods in either HashMap or Hashtable, but it wouldn't be pretty....some kind of mess of contains()...etc.

I think I would either just maintain 2 HashMaps (one in each direction)...or just make up my own Class that utilized 2 HashMaps to store your data

I'm not real excited about this solution....and it seems like too much work for only a dozen or so entries...hmmm.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
If you are not restricted from using other open source jars, you should probably take a look at BiDiMaps
Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
Thanks, Bert, that's exactly what I did! Must be learning something...

I agree. Here's the link: http://aspose.com/file-tools
subject: Lists
It's not a secret anymore!