• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the best way to merge HashMaps in Java?  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I have in my code a few hasMaps



and I want to merge them all in one final map.
I am using Java SE7 , so I cannot use SE8 merge method.

Which is the best way to merge these maps?
Thanks
Ioanna
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a succession of 'putAll' calls. e.g:

Or, redesign your code so you don't end up with multiple Maps that really should be one Map.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will putAll do if you have duplicate keys?

Why are you still using Java7?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What will putAll do if you have duplicate keys?

Of course the existing key/value is replaced with the put key/value. Maybe that's desirable? Maybe it isn't?
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all !
I am using se7 because this is the version of java my company uses,
hope I could use 8 and make my life easier :P
thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!