Java Collections

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:

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();

  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());
  }
 }
}

Output:
100 Dinesh
101 Sweety
103 Nimmo

<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago