• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

how to store multiple values in one key in hash table  RSS feed

 
Ranch Hand
Posts: 72
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a HashMap that maps a particular key to a List
 
Wanderer
Sheriff
Posts: 18671
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ,

will try and let you guys know
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!