CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

Java LinkedHashMap

LinkedHashMap is a class that implements the Map interface.

It stores data as key-value pairs like HashMap, but it additionally maintains a predictable iteration order.

By default, that order is the insertion order.

Inserted:

1 -> Java
2 -> Python
3 -> C++

Iteration:

1 -> Java
2 -> Python
3 -> C++

LinkedHashMap belongs to the java.util package.

import java.util.LinkedHashMap;

Why do we use LinkedHashMap?

LinkedHashMap is useful when:

  • We need key-value storage.
  • Keys must be unique.
  • We want predictable iteration order.
  • We want to preserve insertion order while using Map functionality.

For example:

import java.util.LinkedHashMap;
import java.util.Map;
 
public class HelloWorld {
    public static void main(String[] args) {
        Map<Integer, String> map = new LinkedHashMap<>();
 
        map.put(101, "Ashish");
        map.put(102, "Rahul");
        map.put(103, "Priya");
 
        System.out.println(map);
    }
}

The iteration order follows the order in which entries were inserted.

Creating a LinkedHashMap

LinkedHashMap<Integer, String> map =
    new LinkedHashMap<>();

Or preferably:

Map<Integer, String> map =
    new LinkedHashMap<>();

Adding Elements

Use put().

map.put(1, "Java");
map.put(2, "Python");
map.put(3, "C++");

Insertion Order

Suppose we insert:

map.put(3, "C");
map.put(1, "Java");
map.put(2, "Python");

When iterating, the entries are encountered in insertion order:

3 -> C
1 -> Java
2 -> Python

The keys are not automatically sorted.

This is an important difference between LinkedHashMap and TreeMap.

Updating an Existing Key

If an existing key is added again:

map.put(2, "Python Programming");

Its value is updated.

The entry does not become a newly inserted key in the normal insertion-order mode.

Duplicate Keys

Duplicate keys are not allowed.

map.put(1, "Java");
map.put(1, "Python");

Final mapping:

1 -> Python

Duplicate Values

Duplicate values are allowed.

map.put(1, "Java");
map.put(2, "Java");

This is valid.

Null Keys and Values

LinkedHashMap permits:

  • One null key.
  • Multiple null values.

Example

map.put(null, "Unknown");
map.put(1, null);
map.put(2, null);

Iterating Through LinkedHashMap

for (Map.Entry<Integer, String> entry :
        map.entrySet()) {
 
    System.out.println(
        entry.getKey() + " -> " + entry.getValue()
    );
}

The entries are encountered in insertion order.

Access-Order Mode

One useful feature of LinkedHashMap is that it can also maintain access order.

This can be enabled using its constructor:

LinkedHashMap<Integer, String> map =
    new LinkedHashMap<>(16, 0.75f, true);

The third parameter:

true

enables access-order mode.

In this mode, accessing an existing entry can move that entry toward the end of the iteration order.

For example:

map.put(1, "Java");
map.put(2, "Python");
map.put(3, "C++");
 
map.get(1);

After accessing key 1, the iteration order can become:

2 -> Python
3 -> C++
1 -> Java

This behavior makes LinkedHashMap useful in certain cache-related designs.

Example: Maintaining Order

Suppose an application receives events:

Map<Integer, String> events =
    new LinkedHashMap<>();
 
events.put(1, "Login");
events.put(2, "Search");
events.put(3, "Logout");

When iterating:

1 -> Login
2 -> Search
3 -> Logout

The insertion sequence is preserved.

Important LinkedHashMap Methods

Because LinkedHashMap implements Map, it provides common Map methods such as:

MethodPurpose
put()Adds or updates an entry
get()Retrieves a value
remove()Removes an entry
containsKey()Checks for a key
containsValue()Checks for a value
size()Returns number of entries
isEmpty()Checks whether map is empty
clear()Removes all entries
keySet()Returns keys
values()Returns values
entrySet()Returns key-value entries

LinkedHashMap vs HashMap

LinkedHashMapHashMap
Maintains predictable iteration orderNo guaranteed iteration order
Usually maintains insertion orderOrder should not be relied upon
Has additional ordering-related overheadSimpler internal structure
Useful when order mattersUseful when order does not matter

LinkedHashMap vs TreeMap

LinkedHashMapTreeMap
Maintains insertion/access orderMaintains sorted key order
Does not sort keysSorts keys
Hash-basedTree-based
Basic operations are typically O(1) averageBasic operations are O(log n)

Is LinkedHashMap Thread-Safe?

LinkedHashMap is not synchronized.

If multiple threads access and modify the same map concurrently, appropriate synchronization or a concurrent collection may be required.

Important Points

  • LinkedHashMap implements Map.
  • It stores key-value pairs.
  • Keys are unique.
  • Duplicate values are allowed.
  • One null key is allowed.
  • Multiple null values are allowed.
  • It maintains predictable iteration order.

By default, iteration follows insertion order. It can also be configured for access-order behavior. It is not synchronized.

Key Takeaway

Use LinkedHashMap when you need a Map and the order in which entries are iterated matters.

Map<Integer, String> map =
    new LinkedHashMap<>();

It is especially useful when you want the key-value behavior of a Map while preserving a predictable order.

See you in next page....🙂

← HashMapTreeMap →
← HashMapTreeMap →