Java LinkedHashMap

Java LinkedHashMap

Java LinkedHashMap

A LinkedHashMap collection maintains the insertion order. Although it is somewhat slower than HashMap for adding and deleting the objects from the collection but you will get much faster performance while iterating the elements.

A LinkedHashMap manage the objects using doubly linked list data structure internally. A LinkedHashMap provides all of the optional Map operations and it also allows null elements. This collection framework provides constant time performance for all basic operations.

Iterator returned by the LinkedHashMap is fail-fast, if the map is structurally modified at any time after the iterator is created, the iterator will throw a ConcurrentModificationException.


Some Important Points about LinkedHashMap


  1. A LinkedHashMap extends HashMap class to order the elements in the collection.
  2. Elements are ordered in this collection framework.
  3. It can contain null values as well.
  4. It allows only single null key to be inserted.
  5. It provides constant time performance for all operations like add, remove or retrieval.
  6. LinkedHashMap is slightly slower than the HashMap but provides much faster iteration rate.
  7. LinkedHashMap uses doubly linked list data structure internally.

Internal Hierarchy of LinkedHashMap class

LinkedHashMap extends HashMap class and it implements Serializable and Cloneable interface as well. So let’s see the internal structure.


Java LinkedHashMap



Some important methods of LinkedHashMap class

LinkedHashMap can be used to store objects as key/value pairs but elements in the collection are ordered. It contains lot’s of methods to perform this operation.

LinkedHashMap extends HashMap class, so most of the methods are inherited from the HashMap class.


Method return Signature Method anme and Description
void clear()
Removes all of the mappings from this map.
boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
protected boolean removeEldestEntry(Map.Entry<K,V> eldest)
Returns true if this map should remove its eldest entry.

An example on LinkedHashMap class

Let’s now create a basic example on LinkedHashMap to understand how it works in java programming.
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;


public class LinkedHashMapExample {

 public static void main(String[] args) {
 
 Map<String, String> map = new LinkedHashMap<String, String>();
 
 map.put("name", "ashish");
 map.put("address", "india");
 map.put("mobile", "9999998877");
 map.put(null, null);
 map.put("interest", null);
 
 Set<String> values = map.keySet();
 Iterator<String> it = values.iterator();
 
 System.out.println("Ordered Elements :");
 
 while(it.hasNext()) {
 Object key = it.next();
 System.out.println("Key (" + key + ") : " + map.get(key));
 }
 
 System.out.println("Check whether key(name) exist : " + map.containsKey("name"));
 System.out.println("Check whether value(india) exist : " + map.containsValue("india"));
 
 System.out.println("Size of map : " + map.size());
 
 map.clear();
 
 System.out.println("After clear size of map : " + map.size());
 

 }

}

Related Post

One thought on “Java LinkedHashMap”

Leave a Reply

Your email address will not be published. Required fields are marked *