Forums Register Login

What is the best way to merge HashMaps in Java?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
What will putAll do if you have duplicate keys?

Why are you still using Java7?
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 290 times.
Similar Threads
write a program to create dynamic maps.
HashMap Problem
how to search equalignorecase search with hashmap
determine the input type of the value of the function parameter map
Compare values in HashMap - Efficient Problem Sovling
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:10:20.