Java LinkedHashMap

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java LinkedHashMap class

Java LinkedHashMap class hierarchy 

Java LinkedHashMap class is Hash table and Linked list implementation of the Map interface, with predictable iteration order. It inherits HashMap class and implements the Map interface.

The important points about Java LinkedHashMap class are:

  • A LinkedHashMap contains values based on the key.

  • It contains only unique elements.

  • It may have one null key and multiple null values.

  • It is same as HashMap instead maintains insertion order

LinkedHashMap class declaration

Let's see the declaration for java.util.LinkedHashMap class.

  1. public class LinkedHashMap extends HashMap implements Map  

LinkedHashMap class Parameters

Let's see the Parameters for java.util.LinkedHashMap class.

  • K: It is the type of keys maintained by this map.

  • V: It is the type of mapped values.

Constructors of Java LinkedHashMap class




It is used to construct a default LinkedHashMap.

LinkedHashMap(int capacity)

It is used to initialize a LinkedHashMap with the given capacity.

LinkedHashMap(int capacity, float fillRatio)

It is used to initialize both the capacity and the fillRatio.

LinkedHashMap(Map m)

It is used to initialize the LinkedHashMap with the elements from the given Map class m.

Methods of Java LinkedHashMap class



Object get(Object key)

It is used to return the value to which this map maps the specified key.

void clear()

It is used to remove all mappings from this map.

boolean containsKey(Object key)

It is used to return true if this map maps one or more keys to the specified value.

Java LinkedHashMap Example

  1. import java.util.*;  
  2. class TestCollection14{  
  3.  public static void main(String args[]){  
  5.   LinkedHashMap<Integer,String> hm=new LinkedHashMap<Integer,String>();  
  7.   hm.put(100,"Amit");  
  8.   hm.put(101,"Vijay");  
  9.   hm.put(102,"Rahul");  
  11. for(Map.Entry m:hm.entrySet()){  
  12.    System.out.println(m.getKey()+" "+m.getValue());  
  13.   }  
  14.  }  
  15. }  

Test it Now

Output:100 Amit
       101 Vijay
       102 Rahul

Java LinkedHashMap Example:remove()

  1. import java.util.*;  
  2. public class LinkedHashMapExample {  
  3.    public static void main(String args[]) {  
  4.    // Create and populate linked hash map  
  5.    Map<Integer, String> map = new LinkedHashMap<Integer, String>();           
  6.    map.put(101,"Let us C");  
  7.    map.put(102"Operating System");  
  8.    map.put(103"Data Communication and Networking");  
  9.    System.out.println("Values before remove: "+ map);    
  10.    // Remove value for key 102  
  11.    map.remove(102);  
  12.    System.out.println("Values after remove: "+ map);  
  13.    }      
  14. }  


Values before remove: {101=Let us C, 102=Operating System, 103=Data Communication and Networking}
Values after remove: {101=Let us C, 103=Data Communication and Networking}

Java LinkedHashMap Example: Book

  1. import java.util.*;    
  2. class Book {    
  3. int id;    
  4. String name,author,publisher;    
  5. int quantity;    
  6. public Book(int id, String name, String author, String publisher, int quantity) {    
  7. = id;    
  8. = name;    
  9. = author;    
  10.     this.publisher = publisher;    
  11.     this.quantity = quantity;    
  12. }    
  13. }    
  14. public class MapExample {    
  15. public static void main(String[] args) {    
  16.     //Creating map of Books    
  17.     Map<Integer,Book> map=new LinkedHashMap<Integer,Book>();    
  18.     //Creating Books    
  19.     Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);    
  20.     Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);    
  21.     Book b3=new Book(103,"Operating System","Galvin","Wiley",6);    
  22.     //Adding Books to map   
  23.     map.put(2,b2);  
  24.     map.put(1,b1);  
  25.     map.put(3,b3);  
  27.     //Traversing map  
  28.     for(Map.Entry<Integer, Book> entry:map.entrySet()){    
  29.         int key=entry.getKey();  
  30.         Book b=entry.getValue();  
  31.         System.out.println(key+" Details:");  
  32.         System.out.println(" "" "" "+b.publisher+" "+b.quantity);   
  33.     }    
  34. }    
  35. }    


2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
1 Details:
101 Let us C Yashwant Kanetkar BPB 8
3 Details:
103 Operating System Galvin Wiley 6