aspose file tools*
The moose likes Java in General and the fly likes how to store multiple values in one key in hash table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to store multiple values in one key in hash table" Watch "how to store multiple values in one key in hash table" New topic
Author

how to store multiple values in one key in hash table

Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
Hi,

I am trying to parse and XML file and creating a hash table. I have never used Hash table before.

I want the Hash table to look like this
________________________________________
key value
doctype1 lib1
doctype1 lib2
doctype1 lib3

doctype2 lib1
doctype2 lib4

doctype3 lib2
---------------------------------------------

I tried the following code, but it overwrites my values
--------------------------------------------------------------------
Map map = new HashMap();
// Add some elements
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
map.put("2", "value4");


// List the entries
for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {
Object key = it.next();
System.out.println("the key is" + key.toString() );
Object value = map.get(key);
System.out.println("the value is" + value.toString() );
}
--------------------------------------------------------------------

Any suggestion would be really helpful to me
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You could use a HashMap that maps a particular key to a List


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
For a ready-made solution, you might try using a MultiValueMap from Jakarta Commons Collections.


"I'm not back." - Bill Harding, Twister
Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
Thanks ,

will try and let you guys know
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to store multiple values in one key in hash table
 
Similar Threads
TreeMap Insanity (or is that me?)
HELP - Stuck on a TreeMap key problem
Using a sorted map...
Working with HashMap
1:1 Map class?