LinkedHashMap Tutorial In Java With Example

LinkedHashMap is a type of Collection which takes all the functionalities of HashMap class i.e. it stores our data in a pair such that each element has a key associated with it. The pair of key and value is often known as Entry. As LinkedHashMap is sub-class of HashMap class so it also indirectly extends AbstractMap class and which further implements Map Interface.

In Addition to all the functionalities of HashMap Class, the functionality of maintaining the insertion is added into LinkedHashMap and to attain this  functionality all the entries(key and value) are linked to each other using doubly-linked list. This doubly-linked list maintains the iteration ordering, which is in general the order in which keys were added in the map.


Hierarchy of LinkedHashMap class

LinkedHashMap Class extends HashMap Class which further  extends AbstractMap Class and that Implements Map Interface as shown in following  figure 1.

Dotted arrow shows AbstractMap implements Map Interface and Bold Array shows  LinkedHashMap extends HashMap which further extends AbstractMap class.

Hierarchy of LinkedHashMap class


 LinkedHashMap Methods in JAVA

Let us discuss all the LinkedHashMap methods one by one with Examples in Java.

1. void clear()

This method as the name suggests removes all the entries in the LinkedHashMap, as shown in the following program

import java.util.LinkedHashMap;

import java.util.Map;

public class LinkedHashMapDemo {

public static void main(String[] args) {

Map<String, Integer> linkedHashMapobject = new LinkedHashMap<String, Integer>();

linkedHashMapobject.put("Samsung Grand quattro price ", new Integer(10000));

linkedHashMapobject.put("Micromax canvas price", new Integer(9000));

linkedHashMapobject.put("Sony T2 Ultra price", new Integer(20000));

linkedHashMapobject.put("Nokia Lumia price", new Integer(15000));

linkedHashMapobject.put("Microsoft Lumia price ", new Integer(16000));

// Displaying the contents of the LinkedHashMap before usig clear method

System.out.println("Contents of LinkedHashMap before clear method : " +

linkedHashMapobject);

linkedHashMapobject.clear();

//Displaying the contents of the LinkedHashMap before usig clear method

System.out.println("Contents of LinkedHashMap  after clear method: " +

linkedHashMapobject);
}
}

Output:

Contents of LinkedHashMap before clear method: {Samsung Grand quattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000}

Contents of LinkedHashMap after clear method: {}

2. boolean containsKey(Object key)

This methods checks whether the key given in the argument list is present or not in the LinkedHashMap entries, as shown in the following program .

import java.util.LinkedHashMap;

import java.util.Map;

public class LinkedHashMapDemo {

public static void main(String[] args) {

Map<String, Integer> linkedHashMapobject = new LinkedHashMap<String, Integer>();

linkedHashMapobject.put("Samsung Grand quattro price ", new Integer(10000));

linkedHashMapobject.put("Micromax canvas price", new Integer(9000));

linkedHashMapobject.put("Sony T2 Ultra price", new Integer(20000));

linkedHashMapobject.put("Nokia Lumia price", new Integer(15000));

linkedHashMapobject.put("Microsoft Lumia price ", new Integer(16000));

// Displaying the contents of the LinkedHashMap before usig clear method

System.out.println("Contents of LinkedHashMap before : " + linkedHashMapobject);

//Checking whether Map contains a particular key

System.out.println("\nLinkedHashMap contains 'Nokia Lumia price' as key? : " + linkedHashMapobject.containsKey("Nokia Lumia price"));
}
}

Output:

Contents of LinkedHashMap before : {Samsung Grand quattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000}

LinkedHashMap contains 'Nokia Lumia price' as key? : true

3. boolean containsValue(Object value)

This methods checks whether the value given in the argument list is present or not in the LinkedHashMap entries, as shown in the following program.

import java.util.LinkedHashMap;

import java.util.Map;

public class LinkedHashMapDemo {

public static void main(String[] args) {

Map<String, Integer> linkedHashMapobject = new LinkedHashMap<String, Integer>();

linkedHashMapobject.put("Samsung Grand quattro price ", new Integer(10000));

linkedHashMapobject.put("Micromax canvas price", new Integer(9000));

linkedHashMapobject.put("Sony T2 Ultra price", new Integer(20000));

linkedHashMapobject.put("Nokia Lumia price", new Integer(15000));

linkedHashMapobject.put("Microsoft Lumia price ", new Integer(16000));

// Displaying the contents of the LinkedHashMap before usig clear method

System.out.println("Contents of LinkedHashMap before : " + linkedHashMapobject);

//Checking whether Map contains a particular value

System.out.println("LinkedHashMap contains 16000 as value? : " + linkedHashMapobject.containsValue(16000));
}
}

Output:

Contents of LinkedHashMap before : {Samsung Grand quattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000}

LinkedHashMap contains 16000 as value? : true

Read All LinkedHashMap Methods in JAVA with Example


Example of LinkedHashMap class

Let us discuss Linked Hashmap class with the help of program, following program has been divided into 3 Steps that we will discuss one by one.

import java.util.LinkedHashMap;

import java.util.Map;

public class LinkedHashMapDemo {

public static void main(String[] args) {

Map<String, Integer> linkedHashMapobject = new LinkedHashMap<String, Integer>();// Step 1

linkedHashMapobject.put("Samsung Grand quattro price ", new Integer(10000)); // Step 2

linkedHashMapobject.put("Micromax canvas price", new Integer(9000));

linkedHashMapobject.put("Sony T2 Ultra price", new Integer(20000));

linkedHashMapobject.put("Nokia Lumia price", new Integer(15000));

linkedHashMapobject.put("Microsoft Lumia price ", new Integer(16000));

System.out.println("Contents of LinkedHashMap : " + linkedHashMapobject); //step 3 

System.out.println("\nValues of linkedHashMapobject after iterating over it : \n");

for (String key : linkedHashMapobject.keySet()) {                                              //Step 4

System.out.println(key + “:\t” + linkedHashMapobject.get(key));
}
}
}

Description of Example:

  • In Step 1, we are creating an object linkedHashMap object that is storing key of String type and value of Integer type
  • In Step 2, we are using put method to add the key value pair in the object we have created in step 1.
  • In Step 3 we are printing  the contents of the LinkedHashMap
  • In Step 4 we are using for each loop to iterate over the linkedhashmap object , we are using keySet and get(key) method to diplay all the values of map, we will discuss each method in detail in next topic.

Output:

Contents of LinkedHashMap : {Samsung Grand uattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000}

Values of linkedHashMapobject after iterating over it :

Samsung Grand uattro price :    10000

Micromax canvas price:            9000

Sony T2 Ultra price:              20000

Nokia Lumia price:               15000

Microsoft Lumia price :           16000

Important Points About LinkedHashMap

  • For using LinkedHashMap you must need to import util.LinkedHashMap or its super class.
  • LinkedHashMap only stores Unique values, that is duplicate values are not allowed.
  • LinkedHashMap maintains order, which means it returns the key value pair in the order in which they have been added.
  • LinkedHashMap similar to HashMap allows null key and null value in it,but with a restriction that there can be only one null key and multiple null values.
  • If a key is re-inserted into the map, insertion order of LinkedHashMap object is not affected at all.
  • Due to the added expense of maintaining the linked list performance of LinkedHashMap is slightly below than that of HashMap,
  • LinkedHashMap likewise HashMap is not thread Safe, That is, it is also unsynchronized.

LinkedHashMap Quick Revision Points

  • Data is stores in key value pair
  • Only unique values are allowed
  • Insertion order is maintained
  • It can have only one null key and multiple null values.
  • It is not thread safe
  • It is unsynchronized

Leave a Reply

Your email address will not be published. Required fields are marked *