LinkedHashMap class in collection framework

This class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted.

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)
  1. A LinkedHashMap contains values based on the key. It implements the Map interface and extends HashMap class.
  2. It contains only unique elements.
  3. It may have one null key and multiple null values.
  4. It is same as HashMap instead maintains insertion order.

Hierarchy of LinkedHashMap class:

LinkedHashMap class in collection framework
Apart from the methods inherited from its parent classes, LinkedHashMap defines following methods:
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();


  Set set=hm.entrySet();
  Iterator itr=set.iterator();

   Map.Entry m=(Map.Entry);
   System.out.println(m.getKey()+" "+m.getValue());

100 Dinesh
101 Sweety
103 Nimmo

<<Previous <<   || Index ||   >>Next >>