LinkedHashMap in java

LinkedHashMap:

LinkedHashMap extends the HashMap class and implements the Map interface.

  • It contains the elements in key-value pair form.
  • It maintains insertion order for its elements.
  • It does not allow duplicate keys.
  • A LinkedHashMap can have only one null key and multiple null values.
  • Its initial default capacity is 16 with a load factor of 0.75.
  • It is non-synchronized.

LinkedHashMap class declaration:

public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>

Where:

    • K: The type of keys maintained by the LinkedHashMap.
    • V: The type of mapped values.

LinkedHashMap class Constructors:

Constructor Description
LinkedHashMap() It will create a default LinkedHashMap.
LinkedHashMap(int capacity) It will create and initialize a LinkedHashMap with the given capacity.
LinkedHashMap(int capacity, float loadFactor) It will create and initialize both the capacity and the load factor.
LinkedHashMap(int capacity, float loadFactor, boolean accessOrder) It will create and initialize both the capacity and the load factor with a specified ordering mode.
LinkedHashMap(Map<? extends K,? extends V> m) It will create and initialize the LinkedHashMap with the elements from the given Map class m.

LinkedHashMap class Methods:

Method Description
V get(Object key) It will return the value to which the specified key is mapped.
void clear() It will eliminate or delete all the key-value pairs from a map.
boolean containsValue(Object value) It will return the true if the map maps one or more keys to the specified value.
Set<Map.Entry<K,V>> entrySet() It will return a Set view of the mappings contained in the map.
void forEach(BiConsumer<? super K,? super V> action) It will perform the given action for each entry in the map until all entries have been processed or the action throws an exception.
V getOrDefault(Object key, V defaultValue) It will return the value to which the specified key is mapped or defaultValue if this map contains no mapping for the key.
Set<K> keySet() It will return a Set view of the keys contained in the map
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) It will return true on removing its eldest entry.
void replaceAll(BiFunction<? super K,? super V,? extends V> function) It will replace each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
Collection<V> values() It will return a Collection view of the values contained in this map.

LinkedHashMap example:

LinkedHashMapTest.java

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
 
/**
 * This class is used to show the LinkedHashMap functionality.
 * @author w3schools
 */
public class LinkedHashMapTest {
	public static void main(String args[]){
		//Create LinkedHashMap object.
		Map linkedHashMap = new LinkedHashMap();
 
		//Add objects to the HashSet.
		linkedHashMap.put(4, "Roxy");
		linkedHashMap.put(2, "Sunil");
		linkedHashMap.put(5, "Sandy");
		linkedHashMap.put(1, "Munish");
		linkedHashMap.put(3, "Pardeep");
 
		//Print the LinkedHashMap object.
		System.out.println("LinkedHashMap elements:");
		System.out.println(linkedHashMap);
 
		//Get iterator
		Set set=linkedHashMap.entrySet();  
		Iterator iterator=set.iterator();  
 
		//Print the LinkedHashMap elements using iterator.
		System.out.println("LinkedHashMap elements " +
				"using iterator:");
		while(iterator.hasNext()){
		   Map.Entry mapEntry=(Map.Entry)iterator.next();  
		   System.out.println("Key: " + mapEntry.getKey() + 
				   ", Value: " + mapEntry.getValue());  
		}  
	}
}

Output:

LinkedHashMap elements:
{4=Roxy, 2=Sunil, 5=Sandy, 1=Munish, 3=Pardeep}
LinkedHashMap elements using iterator:
Key: 4, Value: Roxy
Key: 2, Value: Sunil
Key: 5, Value: Sandy
Key: 1, Value: Munish
Key: 3, Value: Pardeep

LinkedHashMap Example:remove():

import java.util.*;  
public class LinkedHashMapExample{  
   public static void main(String args[]) {  
    Map&lt;Integer,String&gt; map=new LinkedHashMap&lt;Integer,String&gt;();        
     map.put(201,"A");    
     map.put(202,"B");    
     map.put(203,"C");    
     System.out.println("Before invoking remove() method: "+map);     
    map.remove(202);  
    System.out.println("After invoking remove() method: "+map);    
   }      
}

Output:

Next Topic: TreeMap in java with example.
Previous Topic: HashMap in java with example.