File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Alternative to Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Alternative to Hashtable" Watch "Alternative to Hashtable" New topic
Author

Alternative to Hashtable

Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
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

Joined: Nov 28, 2001
Posts: 233
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


Sharing is learning
Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
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

Joined: Dec 12, 2002
Posts: 580
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

Joined: Dec 12, 2002
Posts: 580
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

Joined: Jul 05, 2001
Posts: 2370

Nice idea Don!


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Alternative to Hashtable