This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes hashMaps within HashMap to hashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashMaps within HashMap to hashMap" Watch "hashMaps within HashMap to hashMap" New topic

hashMaps within HashMap to hashMap

Timo Leppiniemi

Joined: Apr 08, 2002
Posts: 9

I have this major pain in the butt problem.

I have:

Map dataStorage = new HashMap();
dataStorage = new TreeMap();

which contains variable number of HashMaps, I need to get those
maps and combine the key value pairs to one HashMap.

How can this be done. I have searched through the web, tested a huge
number of different thigs. Maybe I'm blinded to see what is wrong...

Can anyone please help me.

Timo Leppiniemi
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

You have one Map, and the values in that Map are more Maps. You want one single Map containing all the key-value pairs from the little Maps? Here's one way to write it, without generics:

[ February 06, 2007: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

If I'm reading the question correctly then I believe that you need to:
1.) create a destination Map instance to put the results in
2.) iterate through the keySet() of the source map-of-maps
a.) get the value (a map) from the source map-of-maps
b.) putAll() of the value to the destination map
Timo Leppiniemi

Joined: Apr 08, 2002
Posts: 9
Thank you all very very much! I got it working now!
I was amazed, that the solution was so simple. My approach was
a little bit more complex...
I agree. Here's the link:
subject: hashMaps within HashMap to hashMap
It's not a secret anymore!