LinkedHashMap - Java Tutorials

This section contains details about linkedhashmap in Java.



LinkedHashMap in Java

The Class LinkedHashMap is an extension of HashMap with specific feature of retaining the insertion order in the order, in which they were inserted. Also if one inserts the key again into the LinkedHashMap the original orders is retained. 
This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted.
You can also create a LinkedHashMap that returns its elements in the order in which they were last accessed.

The LinkedHashMap class supports five constructors.

LinkedHashMap( ) - This constructor constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).

LinkedHashMap(int capacity) - This constructor constructs an empty LinkedHashMap with the specified initial capacity.

LinkedHashMap(int capacity, float fillRatio) - This constructor constructs an empty LinkedHashMap with the specified initial capacity and load factor.

LinkedHashMap(Map m) - This constructor constructs a insertion-ordered Linked HashMap with the same mappings as the specified Map.

LinkedHashMap(int capacity, float fillRatio, boolean Order) - This constructor construct an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.

The list of methods supported by LinkedHashMap Class are shown in the table given below:

clear( ) -   Removes all mappings from the map.

containsValue(object value ) -  Returns true if this map maps one or more keys to the specified value.

get(Object key)  -   Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

removeEldestEntry(Map.Entry eldest) - Returns true if this map should remove its eldest entry.

Here is the  example for demonstration of the LinkedHashMap -



import java.util.*;

public class LinkedHashMapDemo {
	public static void main(String args[]) {
		LinkedHashMap Lhm = new LinkedHashMap();
		Lhm.put(1, "Gyan");
		Lhm.put(6, "Ankit");
		Lhm.put(5, "Arun");
		Lhm.put(4, "Anand");
		Lhm.put(3, "Ram");
		System.out.println("The Entries of LinkedHashMap are : " + Lhm);

The Entries of LinkedHashMap are :

 {1=Gyan, 6=Ankit, 5=Arun, 4=Anand, 3=Ram}

Share on Google+Share on Google+

LinkedHashMap - Java Tutorials

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: LinkedHashMap is Actually Quite Useful - Java Tutorials  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
October 6, 2011

Regarding the great explanation of LinkedHashmap: I recently used it like this: LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>(); Then a series of 'put' statements were used to load the data. Reason I am pointing this out is that I don't see where this use fits into the "5 constructors" laid out in the article. Is this yet another use of the class?
March 6, 2012
Thank you

Thanks for the clear and crisp explanation!
July 6, 2012
Wedriver Windows popup handling

0 down vote favorite Hi all My code is Set windowsid = driver.getWindowHandles(); Iterator iterate = windowsid.iterator(); while(iterate.hasNext()){ System.out.println(; } String mainwindow_id =; String tabbedwindow_id =; I get an error @ " -- String mainwindow_id =; -- " Stating the below, Line 45 is the one i have in quotes. I have declared the Set as String Please Help ! Exception in thread "main" java.util.NoSuchElementException at java.util.LinkedHashMap$LinkedHashIterator.nextEntry( at java.util.LinkedHashMap$ at windows.traverse.main(