The Class LinkedHashMap is an extension of HashMap with specific feature of retaining the insertion order.
Java collections Framework- LinkedHashMap Class
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 Linked HashMap 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} |
Download This Code.
Here is the Example of methods implementation of LinkedHashMap class.
[ 0 ] Comments