Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Javatutorials LinkedHashMap - Java Tutorials

Related Tutorials


 
 

Share on Google+Share on Google+

LinkedHashMap - Java Tutorials

Advertisement
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 -

Example:- LinkedHashMapDemo.java

package devmanuals.com;

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);

	}
}
Output:-
The Entries of LinkedHashMap are :

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Discuss: LinkedHashMap is Actually Quite Useful - Java Tutorials  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Steve
October 6, 2011
LinkedHashMap

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?
abc
March 6, 2012
Thank you

Thanks for the clear and crisp explanation!
Ram
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(iterate.next()); } String mainwindow_id = iterate.next(); String tabbedwindow_id = iterate.next(); I get an error @ " -- String mainwindow_id = iterate.next(); -- " 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(LinkedHashMap.java:375) at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:384) at windows.traverse.main(traverse.java:45)
DMCA.com