VOOZH about

URL: https://www.geeksforgeeks.org/java/linkedhashmap-class-in-java/

⇱ LinkedHashMap in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

LinkedHashMap in Java

Last Updated : 27 Oct, 2025

LinkedHashMap in Java implements the Map interface of the Collections Framework. It stores key-value pairs while maintaining the insertion order of the entries. It maintains the order in which elements are added.

  • Stores unique key-value pairs.
  • Maintains insertion order.
  • Allows one null key and multiple null values.
  • It is not thread-safe; to synchronize it, use Collections.synchronizedMap().

Declaration of LinkedHashMap

public class LinkedHashMap<K,​V> extends HashMap<K,​V> implements Map<K,​V>

Here, K is the key Object type and V is the value Object type

  • K: The type of keys in the map.
  • V: The type of values mapped in the map.

Output
LinkedHashMap: {Apple=50, Banana=30, Mango=70, Orange=40}

Internal Working of LinkedHashMap

LinkedHashMap extends HashMap and implements the Map interface:

public class LinkedHashMap extends HashMap implements Map

It stores data as nodes similar to a doubly-linked list, maintaining insertion order. Each node contains:

πŸ‘ LinkedHashMap Node Representation in Java

  • Key: Since this class extends HashMap, the data is stored in the form of a key-value pair. Therefore, this parameter is the key to the data.
  • Value: For every key, there is a value associated with it. This parameter stores the value of the keys. Due to generics, this value can be of any form.
  • Next: Since the LinkedHashMap stores the insertion order, this contains the address to the next node of the LinkedHashMap.
  • Previous: This parameter contains the address to the previous node of the LinkedHashMap.

Hierarchy of LinkedHashMap

It extends HashMap and maintains a doubly-linked list to preserve the insertion order of elements.

πŸ‘ Map
LinkedHashMap

Constructors of LinkedHashMap Class

LinkedHashMap class provides various constructors for different use cases:

1. LinkedHashMap()

Creates an empty LinkedHashMap with default initial capacity (16) and load factor (0.75)

LinkedHashMap<K, V> lhm = new LinkedHashMap<>();

2. LinkedHashMap(int initialCapacity, float loadFactor)

Creates a LinkedHashMap with specified capacity and load factor.

LinkedHashMap<K, V> lhm = new LinkedHashMap<>(20, 0.75f);

3. LinkedHashMap(Map<? extends K,​? extends V> map)

Creates a LinkedHashMap containing all elements from the specified map, maintaining their insertion order.

LinkedHashMap<K, V> lhm = new LinkedHashMap<>(existingMap);

Performing Various Operations on LinkedHashMap

Let’s see how to perform a few frequently used operations on the LinkedHashMap class instance.

1. Adding Elements in LinkedHashMap

We can add elements to a LinkedHashMap using the put() method, which retains the insertion order unlike HashMap.


Output
{3=Geeks, 2=For, 1=Geeks}

2. Updating Elements in LinkedHashMap

To update an element in a LinkedHashMap, use the put() method again with the same key and a new value , it replaces the old value while keeping the insertion order.


Output
{3=Geeks, 2=Geeks, 1=Geeks}
Updated Map: {3=Geeks, 2=For, 1=Geeks}

3. Removing Element in LinkedHashMap

We can remove elements from a LinkedHashMap using the remove() method by specifying the key of the element to be deleted.


Output
{3=Geeks, 2=Geeks, 1=Geeks, 4=For}
{3=Geeks, 2=Geeks, 1=Geeks}

4. Iterating through the LinkedHashMap

We can iterate through a LinkedHashMap using a for-each loop over map.entrySet(), accessing each key and value with getKey() and getValue() methods.


Output
3 : Geeks
2 : For
1 : Geeks

Methods of LinkedHashMap

Below are some commonly used methods of the LinkedHashMap class:

MethodDescription
put(K key, V value)Adds or updates a key-value pair in the map.
get(Object key)Returns the value associated with the specified key.
remove(Object key)Removes the mapping for the specified key.
containsKey(Object key)Checks if the map contains the specified key.
containsValue(Object value)Checks if the map contains the specified value.
clear()Removes all key-value pairs from the map.
size()Returns the number of key-value pairs in the map.
isEmpty()Checks if the map is empty.
keySet()Returns a Set view of all keys in the map.
values()Returns a Collection view of all values in the map.
entrySet()Returns a Set view of all key-value mappings in the map.
getOrDefault(Object key, V defaultValue)Returns the value for a key, or a default value if the key is not found.


Comment
Article Tags: