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. The first form constructs a default LinkedHashMap:
LinkedHashMap( )
The second form initializes the LinkedHashMap with the elements from m:
LinkedHashMap(Map m)
The third form initializes the capacity:
LinkedHashMap(int capacity)
The fourth form initializes both capacity and fill ratio. The meaning of capacity and fill ratio are the same as for HashMap:
LinkedHashMap(int capacity, float fillRatio)
The last form allows you to specify whether the elements will be stored in the linked list by insertion order, or by order of last access. If Order is true, then access order is used. If Order is false, then insertion order is used.
LinkedHashMap(int capacity, float fillRatio, boolean Order)
- A LinkedHashMap contains values based on the key. It implements the Map interface and extends HashMap class.
- It contains only unique elements.
- It may have one null key and multiple null values.
- It is same as HashMap instead maintains insertion order.
Hierarchy of LinkedHashMap class:
SN | Methods with Description |
---|---|
1 | void clear() Removes all mappings from this map. |
2 | boolean containsKey(Object key) Returns true if this map maps one or more keys to the specified value. |
3 | Object get(Object key) Returns the value to which this map maps the specified key. |
4 | protected boolean removeEldestEntry(Map.Entry eldest) Returns true if this map should remove its eldest entry. |
Example of LinkedHashMap class:
import java.util.*; class Simple{ public static void main(String args[]){ LinkedHashMap hm=new LinkedHashMap(); hm.put(100,"Dinesh"); hm.put(101,"Sweety"); hm.put(102,"Nimmo"); Set set=hm.entrySet(); Iterator itr=set.iterator(); while(itr.hasNext()){ Map.Entry m=(Map.Entry)itr.next(); System.out.println(m.getKey()+" "+m.getValue()); } } }
100 Dinesh
101 Sweety
103 Nimmo
- ArrayList class
- LinkedList class
- ListIterator interface
- HashSet class
- LinkedHashSet class
- TreeSet class
- Difference between TreeSet & HashSet
- Map interface
- HashMap class
- How does work HashMap?
- Difference between TreeMap vs HashMap
- TreeMap class
- Hashtable class
- Difference between HashMap and HashTable in Java
- Sorting
- Comparable interface