LinkedHashMap - Java Tutorials

This section contains details about linkedhashmap in Java.

LinkedHashMap - Java Tutorials

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}


  1. Assertion in java
  2. Anonymous Inner Classes - Anonymous Inner Classes tutorial
  3. Appending Strings - Java Tutorials
  4. Assertion in Java
  5. Autoboxing unboxing in Java - Java Tutorials
  6. Thread Deadlocks - Java Tutorials
  7. BASIC Java - Java Tutorials
  8. Interthread Communication in Java
  9. boolean comparisons - tutorial
  10. Catching Exceptions in GUI Code - Java Tutorials
  11. Exception in Java - Java Tutorials
  12. Causing Deadlocks in Swing Code
  13. Class names don't identify a class - Java Tutorials
  14. Commenting out your code - Java Tutorials
  15. Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java
  16. Disassembling Java Classes - Java Tutorials
  17. Double-checked locking,java tutorials,java tutorial
  18. Exceptional Constructors - Java Tutorials
  19. Final Methods - Java Tutorials
  20. garbage collection in java
  21. Java - JDK Tutorials
  22. J2EE Singleton Pattern - Design Pattern Tutorials
  23. Java Comments - Java Tutorials
  24. Java Field Initialisation - Java Tutorials
  25. Java HashSet  - Java Tutorials
  26. Java Multi Dimensions Array - Java Tutorials
  27. java awt package tutorial
  28. Java GC
  29. Java HashMap - Java Tutorials
  30. JDK 1.4 the NullPointerException - Java Tutorials
  31. HashMap and HashCode
  32. LinkedHashMap - Java Tutorials
  33. Which is Faster - LinkedList or ArrayList?
  34. Making Enumerations Iterable - JDK 5 Example
  35. Making Exceptions Unchecked - java tutorial,java tutorials
  36. Creation Time Comparison of Multi Dimensional Array- Java Tutorials
  37. Multicasting in Java - java tutorials,tutorial
  38. Non-virtual Methods in Java - java tutorials
  39. Orientating Components Right to Left,java newsletter,java,tutorial
  40. The link to the outer class,java tutorial,java tutorials