Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Map.put and Map.putAll methods?

Map.putAll is equivalent to that of calling Map.put(k, v) on the map once for each mapping from key k to value v in the specified map. So with the functional aspect, both are same.

So, I am curious to know what are the other differences and when to use which one?

like image 997
GuruKulki Avatar asked Apr 28 '11 09:04

GuruKulki


4 Answers

Well, it depends.

put and putAll are interface methods, so every real implementation of that interface will guarantee, that the put method puts a single key/value pair in the map while putAll will put all key/value pairs from the source.

But it's up to the implementor how to do it and what to do in addition (internally).

Sure, a trivial implementation would call put for each and every entry of the source map, but maybe someone invents another method to achieve the goal. Or putAll will do some other map internal stuff before/after/while entering adding pairs.

My rule of a thumb is: if you have to put all key/value pairs from one map to another, then rely on the smartness of the implementor and use the putAll method. There's always a good chance that it provides a better performance than calling put for all pairs manually.

like image 122
Andreas Dolk Avatar answered Oct 12 '22 04:10

Andreas Dolk


As stated in docs:

Map.put

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

Allows you to put a single key-value pair in map.

Map.putAll

Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.

put all data from one map to another map.


when to use which one?

If you want to copy complete data from one map to other you can use map.putAll else you can simply add one key-value pair using map.put.


Map.putAll is equivalent to that of calling Map.put(k, v) on the map once for each mapping from key k to value v in the specified map. So with functional aspect both are same.

No when you implement map in hasmap then to copy one map to another using put(k,v) will take more effort and you can say more coding using putAll(m) we can copy map with a single line code.

like image 44
Harry Joy Avatar answered Oct 12 '22 02:10

Harry Joy


Use putAll(Map) when you have a Map of several values that you want to add to your Map, and use put(K,V) when you have a one or a couple of values you want to add to your Map.

putAll(Map) in most implementations just calls put(K,V) in a loop, read the source.

like image 5
Christoffer Hammarström Avatar answered Oct 12 '22 04:10

Christoffer Hammarström


Since Map is just an interface, without any implementation, there cannot be any difference between a putAll and a repeated put, except in what you call the functional aspect. In other words: there can't be any difference. However, if you look at individual implementations of Map (e.g. HashMap) there may be differences in performance. One putAll should be at least as efficient as a repeated put for any reasonable implementation, but it may be just exactly the same.

like image 5
njlarsson Avatar answered Oct 12 '22 04:10

njlarsson