Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternative to Hashtable

 
Pearlo Muthukumaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My requirement is to have a set of Name = Value pairs where the name itself is repeatable. Which is the best Collection object that could be used for the above requirement. Hashtable / HashMap do not allow duplicate keys therefore are not candidates. I could use String[][] but marshalling data in and out would require my own code ;-). Is there a better alternative?
Thanks in advance
Regards
Muthu
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have one approach to your problem
Have two ArrayList. One to store the Name part and another to store the value part.
In ArrayList you can add the element to the list by passing the index. Make sure the index for name part is same as the index the value part in both the ArrayList.
example. if ip=10.0.0.1 is your name value pair.
then
ArrayList aList1 = new ArrayList();
ArrayList aList2 = new ArrayList();
aList1.add( 1, "ip");
aList2.add(1, "10.0.0.1 ");
now when getting the value back get the index of the "aList1" and use the same index in second list (aList2) to retive the value.
Let me if this approach helps you.
-Arun
 
Pearlo Muthukumaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Arun..
I see a point in Using ArrayList because that is one object which can grow without creating internally a new instance unlike a Vector...

Thanks anyway
Muthu
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use a HashMap of List (probably ArrayList but possibly LinkedList depending on what you want to do with it).
I would use a small class that wraps a HashMap making it typesafe, something like,

In the example above KeyType should be substituted for the type of your key and ValueType should be substituted for the type of the values you are storing. For instance if you are mapping a person's name a String to instances of Person your put method would look like this :

Make sense ?
D.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of enhancements you may want to consider too :
1. Making the get method return a copy of the internal list rather than the list itself. This depends on whether you want clients to be able to change the contents of your map.
2. Making the get method return an empty list rather than null if no value exists for a given key.
(I would probably do both, but it depends on how you want to use it)
D.
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice idea Don!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic