LinkedHashMap in Java

LinkedHashMap

In the last blog, we learned about the HashMap class and its methods. If you want to learn more about HashMap, visit HashMap in Java. In this blog, we will go through the class called LinkedHashMap.


LinkedHashMap

LinkedHashMap works the same as HashMap with additional features. HashMap provides the advantage of quick insertion, deletion, and search but the track and the order of the insertion is never maintained. But the opposite is the case with LinkedHashMap, LinkedHashMap provides the ability where elements can be accessed in their insertion order. In order to use the LinkedHashMap we must import the package called java.util.


Creating LinkedHashMap

e.g.

import java.util.LinkedHashMap;
public class LinkedHashMapEx {
  public static void main(String[] args) {
    LinkedHashMap<String, String> lHashMap = new LinkedHashMap<>();
    lHashMap.put("US", "United States");
    lHashMap.put("UK", "United Kingdom");
    lHashMap.put("IND", "India");
    System.out.println("LinkedHashMap: "+ lHashMap);
  }
}

Output

LinkedHashMap: {US=United States, UK=United Kingdom, IND=India}

Methods of LinkedHashMap

  1. put()
  2. put() method adds or insert the elements in the Hashmap.

  3. putAll()
  4. putAll() method adds or inserts all the key-value mappings from the specified Map to the HashMap.

  5. putIfAbsent()
  6. putIfAbsent() method inserts the specified value with the specified key in the map only if it is not already present in the hashmap.

    Example of LinkedHashMap using the above three methods

    import java.util.LinkedHashMap;
    public class LinkedHashPutMethodsEx {
      public static void main(String[] args) {
        LinkedHashMap<String, String> lHashMap = new LinkedHashMap<>();
        lHashMap.put("US", "United States");
        lHashMap.put("UK", "United Kingdom");
        lHashMap.put("IND", "India");
        lHashMap.put("JP", "Japan");
        System.out.println("LinkedHashMap: "+ lHashMap);
        lHashMap.putIfAbsent("HK", "Hong Kong");
        System.out.println("LinkedHashMap after using putIfAbsent(): "+ lHashMap);
        LinkedHashMap<String, String> lHashMap2 = new LinkedHashMap<>();
        lHashMap2.put("NP", "Nepal");
        lHashMap2.put("PAK", "Pakistan");
        lHashMap2.put("TUR", "Turkey");
        lHashMap2.put("BT", "Butan");
        System.out.println("LinkedHashMap2: "+ lHashMap2);
        lHashMap.putAll(lHashMap2);
        System.out.println("Updated LinkedHashMap: "+ lHashMap);
      }
    }

    Output

    LinkedHashMap: {US=United States, UK=United Kingdom, IND=India, JP=Japan}
    LinkedHashMap after using putIfAbsent(): {US=United States, UK=United Kingdom, IND=India, JP=Japan, HK=Hong Kong}
    LinkedHashMap2: {NP=Nepal, PAK=Pakistan, TUR=Turkey, BT=Butan}
    Updated LinkedHashMap: {US=United States, UK=United Kingdom, IND=India, JP=Japan, HK=Hong Kong, NP=Nepal, PAK=Pakistan, TUR=Turkey, BT=Butan}
  7. entrySet()
  8. entrySet() method returns a set of all the key-value mapping of the map.

  9. keySet()
  10. The keySet() method is used to return a set view of the keys present in this map.

  11. values()
  12. values() method returns the view of all the values present in the entries of the hashmap.

    Example of LinkedHashMap using the above three methods

    import java.util.LinkedHashMap;
    public class LinkedHashDisplayingKeyValuesEx {
      public static void main(String[] args) {
        LinkedHashMap<String, String> lHashMap = new LinkedHashMap<>();
        lHashMap.put("US", "United States");
        lHashMap.put("UK", "United Kingdom");
        lHashMap.put("IND", "India");
        lHashMap.put("JP", "Japan");
        System.out.println("LinkedHashMap: "+ lHashMap);
        System.out.println("LinkedHashMap after using entrySet(): "+ lHashMap.entrySet());
        System.out.println("LinkedHashMap after using keySet(): "+ lHashMap.keySet());
        System.out.println("LinkedHashMap after using values(): "+ lHashMap.values());
      }
    }

    Output

    LinkedHashMap: {US=United States, UK=United Kingdom, IND=India, JP=Japan}
    LinkedHashMap after using entrySet(): [US=United States, UK=United Kingdom, IND=India, JP=Japan]
    LinkedHashMap after using keySet(): [US, UK, IND, JP]
    LinkedHashMap after using values(): [United States, United Kingdom, India, Japan]
  13. get()
  14. get() method returns the value of the specified key in the hashmap.

  15. getOrDefault()
  16. The getOrDefault() method returns the value associated with the given key. If the key cannot be discovered, it returns the supplied default value.

  17. remove()
  18. remove() method removes or deletes the specified values with the associated specified keys from the map.

    Example for LinkedHashMap using the above two methods

    import java.util.LinkedHashMap;
    public class LinkedHashGetAndRemoveMethodsEx {
      public static void main(String[] args) {
        LinkedHashMap<String, String> lHashMap = new LinkedHashMap<>();
        lHashMap.put("US", "United States");
        lHashMap.put("UK", "United Kingdom");
        lHashMap.put("IND", "India");
        lHashMap.put("JP", "Japan");
        System.out.println("LinkedHashMap: "+ lHashMap);
        System.out.println("LinkedHashMap after using get(): "+ lHashMap.get("UK"));
        System.out.println("LinkedHashMap after using getOrDefault(): "+ lHashMap.getOrDefault("MV", "Maldives"));
        System.out.println("Removed value: "+ lHashMap.remove("JP"));
        System.out.println("Updated LinkedHashMap: "+ lHashMap);
      }
    }

    Output

    LinkedHashMap: {US=United States, UK=United Kingdom, IND=India, JP=Japan}
    LinkedHashMap after using get(): United KingdomLinkedHashMap after using getOrDefault(): Maldives
    Removed value: Japan
    LinkedHashMap: {US=United States, UK=United Kingdom, IND=India}
Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.