LinkedHashMap - Java Tutorials

This section contains details about linkedhashmap in Java.



Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion 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(