How to putAll on Java hashMap contents of one to another, but not replace existing keys and values?

后端 未结 7 1635
猫巷女王i
猫巷女王i 2020-12-01 00:20

I need to copy all keys and values from one A HashMap onto another one B, but not to replace existing keys and values.

Whats the best way to do that?

I was t

相关标签:
7条回答
  • 2020-12-01 01:26

    You can make it in just 1 line if you change maps order in @erickson's solution:

    mapWithNotSoImportantValues.putAll( mapWithImportantValues );
    

    In this case you replace values in mapWithNotSoImportantValues with value from mapWithImportantValues with the same keys.

    0 讨论(0)
提交回复
热议问题