Java LinkedHashMap

Last Updated : 28 Mar 2026

Java LinkedHashMap is used to store data in key-value pairs while maintaining the insertion order. It is useful when you need both fast access and ordered data. It is a part of the Java Collection Framework.

In this chapter, you will learn about the Java LinkedHashMap class, its features, and examples.

What is LinkedHashMap in Java?

Java LinkedHashMap class is a Hashtable and Linked List implementation of the Map interface. It extends the HashMap class and implements the Map interface.

It stores data as key-value pairs and maintains the order in which elements are inserted.

Java LinkedHashMap class hierarchy

Features of LinkedHashMap

Here are some of the key features of LinkedHashMap:

  • Java LinkedHashMap stores values based on keys.
  • It contains unique keys.
  • It allows one null key and multiple null values.
  • It is non-synchronized.
  • It maintains insertion order.
  • The default capacity is 16 with a load factor of 0.75.

LinkedHashMap class declaration

Let's see the declaration for java.util.LinkedHashMap class.

LinkedHashMap class Parameters

Let's see the parameters for java.util.LinkedHashMap class:

  • K: It is the type of keys maintained by this map.
  • V: It is the type of mapped values.

Constructors of LinkedHashMap Class

Java LinkedHashMap class provides different constructors to create and initialize a LinkedHashMap.

1. LinkedHashMap()

It is used to construct a default LinkedHashMap.

Here is the syntax:

2. LinkedHashMap(int capacity)

It is used to construct a LinkedHashMap with the specified initial capacity.

Here is the syntax:

3. LinkedHashMap(int capacity, float loadFactor)

It is used to construct a LinkedHashMap with the specified capacity and load factor.

Here is the syntax:

4. LinkedHashMap(int capacity, float loadFactor, boolean accessOrder)

It is used to construct a LinkedHashMap with the specified capacity, load factor, and ordering mode (insertion order or access order).

Here is the syntax:

5. LinkedHashMap(Map m)

It is used to construct a LinkedHashMap containing the elements of the specified Map.

Here is the syntax:

Methods of LinkedHashMap class

The following table shows the commonly used methods of the LinkedHashMap class:

MethodDescription
V get(Object key)It returns the value to which the specified key is mapped.
void clear()It removes all the key-value pairs from a map.
boolean containsValue(Object value)It returns true if the map maps one or more keys to the specified value.
Set<Map.Entry<K,V>> entrySet()It returns a Set view of the mappings contained in the map.
void forEach(BiConsumer<? super K,? super V> action)It performs the given action for each entry in the map until all entries have been processed or the action throws an exception.
V getOrDefault(Object key, V defaultValue)It returns the value to which the specified key is mapped or defaultValue if this map contains no mapping for the key.
Set<K> keySet()It returns a Set view of the keys contained in the map
protected boolean removeEldestEntry(Map.Entry<K,V> eldest)It returns true on removing its eldest entry.
void replaceAll(BiFunction<? super K,? super V,? extends V> function)It replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
Collection<V> values()It returns a Collection view of the values contained in this map.
 

Examples of LinkedHashMap

Here are some examples of the Java LinkedHashMap class to understand its usage.

Example 1: Simple LinkedHashMap Example

In this example, we are creating a LinkedHashMap, adding elements, and traversing it.

Output:

100 Amit
101 Vijay
102 Rahul

Example 2: Accessing Keys and Values

In this example, we are fetching keys, values, and key-value pairs from LinkedHashMap.

Output:

Keys: [100, 101, 102]
Values: [Amit, Vijay, Rahul]
Key-Value pairs: [100=Amit, 101=Vijay, 102=Rahul]

Example 3: Removing Elements from LinkedHashMap

In this example, we are removing elements using the remove() method.

Output:

Before invoking remove() method: {101=Amit, 102=Vijay, 103=Rahul}
After invoking remove() method: {101=Amit, 103=Rahul}

Example 4: Storing Objects in LinkedHashMap

In this example, we are storing user-defined Book objects in a LinkedHashMap.

Output:

2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
1 Details:
101 Let us C Yashwant Kanetkar BPB 8
3 Details:
103 Operating System Galvin Wiley 6