LinkedHashMap in Java - Javastudypoint

Saturday, March 23, 2019

LinkedHashMap in Java

LinkedHashMap in Java is a class which extends the HashMap. Java LinkedHashMap is a combination of a hash table and a linked list of the map interface. It allows insertion order iteration over the map, that means the elements will be returned in the order in which they are inserted.
LinkedHashMap in Java


Points to remember about Java LinkedHashMap:

1. implements the Map interface and extends the Java HashMap class.
2. It contains values based on the key.
3. The underlying data structure is LinkedList and Hashtable.
4. It maintains the insertion order.
5. It may have one null key and multiple null values.


Constructors in Java LinkedHashMap class:

Java LinkedHashMap class defines the following 5 constructors.
1. LinkedHashMap(): It creates a default LinkedHashMap with initial capacity is 16 and the load factor is 0.75
2. LinkedHashMap(int initial capacity): It initializes the LinkedHashMap with the specified initial capacity.
3. LinkedHashMap(int initial capacity, float loadFactor):  It initializes the LinkedHashMap with the specified initial capacity and load factor.
4. LinkedHashMap(Map map): It initializes the LinkedHashMap with the elements of the specified map.
5. LinkedHashMap(int initial capacity, float loadFactor, boolean Order): It initializes the LinkedHashMap with the specified initial capacity, load factor, and ordering mode. True is passed for last access order and false is passed for insertion order. 

Methods in Java LinkedHashMap class:

1. void clear(): removes all the key/values pair from the map.
2. boolean containsKey(Object key): returns true if the map contains a specified key.
3. boolean containsValue(Object value): returns true if the map contains a specified value.
4. Object get(Object key): It returns a value mapped by the specified key.
5. Set keySet(): It returns a set of keys in the invoking map.
6. Set entrySet(): It returns a set that contains the entries in the map.
7. Object getOrDefault(Object key, Object defaultValue): It returns the value to which the specified key mapped or return a default value if the map contains no mapping for the key.
8. protected boolean removeEldestEntry(Map.Entry eldest): This method returns true on removing map eldest entry.
9. Collection values(): This method returns a collection view of the values contained in this map.

Java LinkedHashMap Example


import java.util.*;
class LinkedhashMapExample{
 public static void main(String args[]){
  
  //creating a hash map
  LinkedHashMap<Integer, String> lmap = new LinkedHashMap<Integer, String>();
  
  //adding values in this map
  lmap.put(101, "Rahul");
  lmap.put(107, "Amit");
  lmap.put(103, "Vijay");
  lmap.put(105, "Suresh");
  lmap.put(102, "John");
  lmap.put(106, "Prashant");
  
  //initially print linked hash map entries
  System.out.println("Linked Hash Map entries are: "+lmap);
  
  //returns true if the specified key found in the map.
  System.out.println("contain (105) key or not: "+lmap.containsKey(105));
  
  //returns true if the specified value found in the map.
  System.out.println("contain specified value (Suresh) or not: "+lmap.containsValue("Suresh"));
  
  // returns the value associated with the key.
  System.out.println("return value of specified key: "+lmap.get(106));
  
  //returns the number of key/values pair
  System.out.println("Linked Hash Map size is: "+lmap.size());
  
  //returns a set that contains the keys 
  System.out.println("Set that contains keys: "+lmap.keySet());
  
  //returns a set that contains the entries 
  Set s = lmap.entrySet();
     System.out.println("Entry set is: "+s);
  
 }
}

Output:
LinkedHashMap in Java with example




Java LinkedHashmap Example to remove the eldest entry:

import java.util.*;
class RemoveEldestEntry{
 
 //refers to maximum size
 private static final int MAX=5;
 
 public static void main(String args[]){
  
  //creating a linked hash map
  LinkedHashMap<Integer, String> lmap = new LinkedHashMap<Integer, String>(){
   
   //removing map eldest entry
   protected boolean removeEldestEntry(Map.Entry<Integer,String> eldest){
   return size() > MAX;
  } 
  };
  
  //adding values in this map
  lmap.put(101, "Rahul");
  lmap.put(102, "Amit");
  lmap.put(103, "Vijay");
  lmap.put(104, "Suresh");
  lmap.put(105, "Prashant");
  
  //initially print linked hash map entries
  System.out.println("Linked Hash Map entries are:n"+lmap);
  
  //adding new element
  lmap.put(106,"John");
  System.out.println("After removing eldest entry and insert new entry:n"+lmap);
  
  //adding new element
  lmap.put(107,"Tom");
  System.out.println("After removing eldest entry and insert new entry:n"+lmap);
 }
}

Output:
LinkedHashMap in Java with example



You may also enjoy these related posts:

How to use ArrayDeque with Example?
Queue interface in Java with Example
Comparable vs Comparator interface in Java Collection
TreeSet in Java with Example
How to implement a stack data structure in Java?
How to add or remove elements from ArrayList?

No comments:

Post a Comment