Difference between Map.put and Map.putAll methods?

40,312

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

The most obvious difference is the synchronized collections.

For a synchornized map, putAll will add all entries as a single operation. If you have two threads attempting to putAll the same keys with different values, you will only get one complete set of values. i.e. either from the first or second thread, but not some combination.

If you use put() repeatedly in two threads, you can get any combination of values being retained which may not be a valid combination.


I have seen/implemented transactional operations for put() and putAll(). When putAll is transactional, all or none key/values will be added. e.g. if a key or value cannot be added for some reason. If you are using put() only the indiviudal key/value (and possibly any not added) will be stopped, performing a potentially incomplete update.

Share:
40,312
GuruKulki
Author by

GuruKulki

Updated on July 09, 2022

Comments

  • GuruKulki
    GuruKulki almost 2 years

    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?

  • Stephen C
    Stephen C about 13 years
    +1 - you beat me ... and you answered the OP's question instead of just reciting the API details.
  • Paul Cager
    Paul Cager about 13 years
    That last line is a bit misleading. Although AbstractMap does work as you describe, other concrete Map implementations do not.
  • Christoffer Hammarström
    Christoffer Hammarström about 13 years
    @Paul: +1, you're right. It's a bit of a simplification, but i believe that most Map interface compliant implementations work that way, as the Map interface doesn't give that much leeway.