What order of elements is inserted in LinkedHashMap.putAll ()?

I read the Javadoc for LinkedHashMap , which mentions:

The putAll method generates one write access for each mapping to the specified card, in the order in which key-value mappings are provided to the specified iterator of the specified card record.

My question is: what does it mean "one write access for each mapping". It would be helpful if someone could help provide an example to clarify this.

+4
source share
3 answers

This paragraph applies to maps created using a special constructor that makes an iteration order based on the last access order (against the insertion order for the standard LinkedHashMap.

It simply says that if the K key is on the map and you call putAll(someOtherMap) , where someOtherMap contains K too, this will be considered access to K and it will be moved to the end of the map (in terms of iterative order).

In other words, in terms of access, putAll equivalent to for (Entry e : entries) map.put(e); (in pseudo-code).

Thoughtful example:

 public static void main(String[] args) throws Exception { Map<String, String> m = new LinkedHashMap<> (16, 0.75f, true); m.put("a", "a"); m.put("b", "b"); System.out.println("m = " + m); // a, b m.put("a", "a"); System.out.println("m = " + m); // b, a Map<String, String> m2 = new LinkedHashMap<>(); m2.put("b", "b"); m.putAll(m2); System.out.println("m = " + m); // a, b: putAll was considered as an access // and the order has changed } 
+3
source

The comment ("The putAll method generates one write access for each mapping on the specified map in the order in which the key-value mappings are provided by the specified iterator of the given dataset.") In the API documentation should be looked into the context. Here is the complete API document with context:

A special constructor is provided for creating a linked hash map, the iteration order is the order in which its entries were the last available , from the least recently accessible to the most recent (access order). Such a map is well suited for creating LRU caches. Calling the put or get method results in access to the corresponding record (if it exists after the call is completed). The putAll method generates one write access for each mapping on the specified card in the order in which key-value mappings are provided by the specified iterator of the specified card record. No other methods generate write access ...

This section describes what defines "access", which affects the definition of "last access". In this context, it describes how put / get and putall are processed regarding access to mappings. The positions (k, v) and get (k) are considered as access to each. Similarly, putAll () is considered as one access for all mappings in the order supported by the recordset. You can imagine this as, for each putAll (), all access mappers for the mappings will be 1, in the order supported by the recordset.

Hope this is what you are looking for.

+3
source

LinkedHashMap keep the order in which you place the item in it. The putAll method is used to copy all the mappings from the specified card to this card.

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

โ€œone write access for each matchโ€ means that the effect of calling putall is equivalent to the effect of calling put (k, v) on this map once for each mapping from the key k to the value v on the specified map.

+1
source

All Articles