Linkedhashmap java example: LinkedHashMap in Java is a class that 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, which means the elements will be returned in the order in which they are inserted.
- LinkedHash Map Class Declaration
- Hierarchy of LinkedHash Map
- Points to remember about Java LinkedHashMap Class
- Constructors of LinkedHash Map
- Methods in Java LinkedHashMap Class
- Java Linked HashMap Example
- Java LinkedHashmap Example to remove the Eldest Entry
LinkedHash Map Class Declaration
Java linkedhashmap example: Declaration of java.util.LinkedHashMap class is as such
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
Parameters of java.util.LinkedHashMap class
K: Type of keys maintained by this map.
V: It is the type of Mapped Values.
Hierarchy of LinkedHash Map

Points to remember about Java LinkedHashMap Class
- Implements the Map interface and extends the Java HashMap class.
- It contains values based on the key.
- The underlying data structure is LinkedList and Hashtable.
- It maintains the insertion order.
- It may have one null key and multiple null values.
Do Check:
Constructors of LinkedHash Map
To Create a LinkedHash Map we need to create an object of LinkedHash Map Class. LinkedHash Map includes several constructors that allow the creation of Array List. Java LinkedHashMap Class defines the following 5 constructors.
| Constructor | Description |
|---|---|
| LinkedHashMap() | It creates a default LinkedHashMap with an initial capacity is 16 and a load factor is 0.75 |
| LinkedHashMap(int initial capacity) | It initializes the LinkedHashMap with the specified initial capacity. |
| LinkedHashMap(int initial capacity, float load factor) | It initializes the LinkedHashMap with the specified initial capacity and load factor. |
| LinkedHashMap(Map map) | It initializes the LinkedHashMap with the elements of the specified map. |
| LinkedHashMap(int initial capacity, float load factor, boolean Order) | It initializes the LinkedHashMap with the specified initial capacity, load factor, and ordering mode. True is passed for the last access order and false is passed for the 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 returns 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 Linked HashMap 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:

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:
