LinkedHashMap is a combination of hash table and linked list that implement the Map interface with predictable iteration order. The difference between HashMap and LinkedHashMap is that LinkedHashMap maintains a doubly-linked list which allows scanning through all of its entries back and forth. The order is maintained, meaning the order in which keys were inserted into the map. The order will not be affected if a key is re-inserted into the map. LinkedHashMap provides all of the optional Map operations and also permits null elements. In terms of time complexity, just like HashMap, it provides constant-time performance for the basic operations such as add, contains and remove. LinkedHashMap has two parameters that affect its performance and they are initial capacity and load factor.There iterations in this class are unaffected by capacity, making it a bit more efficient than HashMap in terms of choosing an excessively high value for initial capacity.
Why is LinkedHashMap useful
- It will iterate through the entries in the order they were put into the map.
- Just like in HashMap, null values are allowed.
- Uses doubly-linked linked lists which makes scanning efficient.
- It has additional awareness towards the order at which the items were added or accessed.
Inheritance Diagram
Constructors summary in LinkedHashMap
- LinkedHashMap(): constructs an empty insertion-ordered LinkedHashMap with default initial capacity (16) and default load factor (0.75).
- LinkedHashMap(int initialCapacity): constructs an empty insertion-ordered LinkedHashMap and sets the capacity to the specified initialCapacity argument and default load factor (0.75.
- LinkedHashMap(int initialCapacity, float loadFactor): constructs an empty insertion-ordered LinkedHashMap with the specified capacity and load factor.
- LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder): construct an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.
- LinkedHashMap(Map <? extends K, ? extends V> m): constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map.
Methods in LinkedHashMap class
- void clear(): removes all of the mappings from this map.
- boolean containsValue(Object value): returns true if the LinkedHashMap contains the specified value, false otherwise.
- Set<Map.Entry<K,V>> entrySet(): returns a set view of the mappings contained in the current map.
- V get(Object key): returns the value to which the specified key is mapped or null if the map does not contain mapping for the key.
- V getOrDefault(Object key, V defaultValue): returns the value to which the specified key is mapped or defaultValue if the map does not contain mapping for the key.
- Set<K> keySet: returns a set view of the keys contained in this map.
- protected boolean removeEldestEntry(Map. Entry<K, V> eldest): returns a collection view of the values contained in this map.
- void put(K key, V value): associates the specified Value to the specified Key. (it is inherited from Map class in Java)
For more information on the main methods of EnumSet, feel free to visit the original Oracle documentation.
Getting the size of a LinkedHashMap, checking whether it contains a certain value in a specific key and checking whether it’s empty and finally, removing a key from a LinkedHashMap:
import java.util.*; public class LinkedHashMapExample { public static void main(String args[]) { LinkedHashMap<String, String> student = new LinkedHashMap<String, String>(); student.put("name", "Joe"); student.put("major", "Computer Science"); student.put("marital status", "Single"); System.out.println(student); // printing the value of the key called name System.out.println("Key 'name's value: " + student.get("name")); // getting the size of the linkedHashMap (size() is inherited from Map) System.out.println("Size of the LinkedHashMap: " + student.size()); // checking whether the map is empty or not System.out.println("Is the map empty: " + student.isEmpty()); // checking whether the linkedHashMap contains the key specified as an argument System.out.println("Does it contain 'marital status'? "+ student.containsKey("marital status")); // deleting/removing an element from the linkedHashMap works by using the //remove method System.out.println("Deleting element 'name': " + student.remove("name")); } }
Output:
{name=Joe, major=Computer Science, marital status = Single} Key 'name's value: Joe Size of the LinkedHashMap: 3 Is the map empty: false Does it contain 'marital status'? true Deleting element 'name': "Joe"
Clearing a LinkedHashMap using clear()
import java.util.*; public class LinkedHashMapExample { public static void main(String[] args) { LinkedHashMap<String, String> student = new LinkedHashMap<String, String>(); li_hash_map.put("name", "Joe"); li_hash_map.put("major", "Computer Science"); li_hash_map.put("marital status", "Single"); System.out.println("Current stage of linkedHashMap: " + student); // Clearing the linked hash map using clear() li_hash_map.clear(); System.out.println("Stage after the clear() method: " + student); } }
Output:
Current stage of linkedHashMap: {"name"="Joe", "major"="Computer Science", "marital status ="Single"} Stage after the clear() method: {}