Java LinkedHashMap Example

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

  1. It will iterate through the entries in the order they were put into the map.
  2. Just like in HashMap, null values are allowed.
  3. Uses doubly-linked linked lists which makes scanning efficient.
  4. It has additional awareness towards the order at which the items were added or accessed.

Inheritance Diagram

Inheritance diagram

Inheritance diagram

Constructors summary in LinkedHashMap

  1. LinkedHashMap(): constructs an empty insertion-ordered LinkedHashMap with default initial capacity (16) and default load factor (0.75).
  2. LinkedHashMap(int initialCapacity): constructs an empty insertion-ordered LinkedHashMap and sets the capacity to the specified initialCapacity argument and default load factor (0.75.
  3. LinkedHashMap(int initialCapacity, float loadFactor): constructs an empty insertion-ordered LinkedHashMap with the specified capacity and load factor.
  4. LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder): construct an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.
  5. 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

  1. void clear(): removes all of the mappings from this map.
  2. boolean containsValue(Object value): returns true if the LinkedHashMap contains the specified value, false otherwise.
  3. Set<Map.Entry<K,V>> entrySet(): returns a set view of the mappings contained in the current map.
  4. 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.
  5. 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.
  6. Set<K> keySet: returns a set view of the keys contained in this map.
  7. protected boolean removeEldestEntry(Map. Entry<K, V> eldest):  returns a collection view of the values contained in this map.
  8. 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"); 
        // 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")); 


{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() 
        System.out.println("Stage after the clear() method: " + student); 


Current stage of linkedHashMap: {"name"="Joe", "major"="Computer Science", "marital status ="Single"}
Stage after the clear() method: {}
























0 0 votes
Article Rating
Inline Feedbacks
View all comments