• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lists

 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks,
Nate
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are not restricted from using other open source jars, you should probably take a look at BiDiMaps
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bert, that's exactly what I did! Must be learning something...

Nate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic