Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to store multiple values in one key in hash table

 
Raj kalaria
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a HashMap that maps a particular key to a List
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a ready-made solution, you might try using a MultiValueMap from Jakarta Commons Collections.
 
Raj kalaria
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ,

will try and let you guys know
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic