LinkedHashMap is used to store key-value pairs very similar to HashMap class. LinkedHashMap is a hash table and doubly linked List based implementation of Map interface, it extends the HashMap class. The iteration order in a LinkedHashMap is normally the order in which the elements are inserted. However, it also provides a special constructor using which you can change the iteration order from the least-recently accessed element to the most-recently accessed element and vice versa. This kind of iteration order can be useful in building LRU caches. In any case, the iteration order is predictable. Important points to about LinkedHashMap
- A LinkedHashMap cannot contain duplicate keys.
- LinkedHashMap can have null values and the null key.
- Unlike HashMap, the iteration order of the elements in a LinkedHashMap is predictable.
- Just like HashMap, LinkedHashMap is not thread-safe.
- It maintains the order of K,V pairs inserted to it by adding elements to internally managed doubly-linked list.
Insertion ordered LinkedHashMap
By default, LinkedHashMap is insertion ordered. It maintains the order of elements when they were added to it. While iterating over LinkedHashMap, we get the KV pairs in exact order they were added.
LinkedHashMap<Integer, String> pairs = new LinkedHashMap<>(); pairs.put(1, "A"); pairs.put(2, "B"); pairs.put(3, "C"); pairs.put(4, "D"); pairs.forEach((key, value) -> { System.out.println("Key:"+ key + ", Value:" + value); }); // Program Output. Key:1, Value:A Key:2, Value:B Key:3, Value:C Key:4, Value:D
Access ordered LinkedHashMap
In access ordered map, keys are sorted on the basis of access order last time they were accessed using any method of LinkedHashMap. Invoking the put, putIfAbsent, get, getOrDefault, compute, computeIfAbsent, computeIfPresent, or merge methods results in an access to the corresponding entry. The keys are sorted from least recently accessed used to most recently accessed and build a LRU cache. To create access order LinkedHashMap set constructor argument to true.
// Access ordered LinkedHashMap Example, 3rd parameter set access order LinkedHashMap<Integer, String> pairs = new LinkedHashMap<>(2, .75f, true); pairs.put(1, "A"); pairs.put(2, "B"); pairs.put(3, "C"); pairs.put(4, "D"); // Access 3rd pair pairs.get(3); // Access 1st pair pairs.getOrDefault(2, "oops"); pairs.forEach((key, value) -> { System.out.println("Key:"+ key + ", Value:" + value); }); // Program Output. Key:1, Value:A Key:4, Value:D Key:3, Value:C Key:2, Value:B
LinkedHashMap Methods
The important methods we should learn about LinkedHashMap are as follows:
- void clear(): It removes all the key-value pairs from the map.
- void size(): It returns the number of key-value pairs present in this map.
- void isEmpty(): It returns true if this map contains no key-value mappings..
- boolean containsKey(Object key): It returns ‘true’ if a specified key is present in the map.
- boolean containsValue(Object key): It returns ‘true’ if a specified value is mapped to at least one key in the map.
- Object get(Object key): It retrieves the value mapped by the specified key.
- Object remove(Object key): It removes the key-value pair for the specified key from the map if present.
- entrySet(): This method returns a Set view of the mappings contained in this map.
- getOrDefault(Object key, V defaultValue): This method returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
- keySet(): This method returns a Set view of the keys contained in this map.
- values(): This method returns a Collection view of the values contained in this map.