LinkedHashMap in Java with example

We are going to about LinkedHashMap in Java, in the given tutorial.

LinkedHashMap

  • LinkedHashMap is a Hash table and linked list implementation with the feature of maintaining an order of elements inserted into it.
  • It consists of only unique elements.
  • There are values based on keys.
  • LinkedHashMap implementation class of Map interface and It extends HashMap class.
  • LinkedHashMap has an additional feature of maintaining the insertion order of the elements. ( This is the one important difference in HashMap and LinkedHashMap, because in HashMap insertion oreder of elements are not maintained).

 

Declaration :

LinkedHashMap<KeyDataType,ValueDataType> l = new LinkedHashMap<KeyDataType,ValueDataType>();

Constructors in LinkedHashMap :

  1. LinkedHashMap() : Default constructor.
  2. LinkedHashMap(int capacity) : Can specify the capacity and initialize particular LinkedHashMap.
  3. LinkedHashMap(Map x) : Can initialize a LinkedHashMap with the elements of Map provided in the argument of the method.
  4. LinkedHashMap(int capacity, int fillRratio) : Can initialize the capacity and the fill ratio to the provided values in the argument for LinkedHashMap. (Fill ratio has to be in the range of 0.0 to 1.0 .)
  5. LinkedHashMap(int capacity, int fillRatio, boolean order) : Can initialize the capacity of Map and the fill ratio for a linked HashMap. And also takes the instruction of following the instruction order through the boolean value ‘order’. True is for last access order and false is for insertion order.

 

Program :

Let’s see a program to understand LinkedHashMap thoroughly :

package java;
import java.util.*;

public class LHM_Example
{
 public static void main(String[] args)
 {
  //HashMap Declaration
  LinkedHashMap<Integer,String> age = new LinkedHashMap<Integer,String>();

  //Adding student's age as kay & name as value to LinkedHashMap
  age.put(18, "Student A");
  age.put(19, "Student B");
  age.put(20, "Student C");
  age.put(21, "Student D");
  age.put(22, "Student E");

  //We are Generating a set of entries
  Set set = age.entrySet();

  //We are Displaying elements of LinkedHashMap with the use of iterator
  Iterator it = set.iterator();

  while(it.hasNext())
  {
   Map.Entry m = (Map.Entry)it.next();
   System.out.println("Key : "+m.getKey()+" Value : "+m.getValue());
  }
  
  //Getting random element value
  System.out.println("Get name of student having age 20: "+age.get(20));

  //Getting size of Map
  System.out.println("Size of the Map: "+age.size());
 
  //Checking if given Map is empty or not
  System.out.println("Is map empty? "+age.isEmpty());

  //Deleting Random element
  System.out.println("Delete student having age 19: "+age.remove(19));

  //Printing elements
  System.out.println(age);

 }
}

Output :

The output of the above program will be :

Key : 18 Value : Student A
Key : 19 Value : Student B
Key : 20 Value : Student C
Key : 21 Value : Student D
Key : 22 Value : Student E
Get name of student having age 20: Student C
Size of the Map: 5
Is map empty? false
Delete student having age 19: Student B
{18=Student A, 20=Student C, 21=Student D, 22=Student E}

 

Leave a Reply

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