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 LinkedHashSet

LinkedHashSet is a Set implementation that combines the uniqueness behavior of HashSet with predictable insertion-order iteration.

It does not allow duplicate elements, and when you iterate through the Set, elements are normally returned in the order in which they were inserted.

import java.util.LinkedHashSet;
 
public class HelloWorld {
    public static void main(String[] args) {
 
        LinkedHashSet<String> languages = new LinkedHashSet<>();
 
        languages.add("Java");
        languages.add("Python");
        languages.add("C++");
 
        System.out.println(languages);
    }
}

Output

[Java, Python, C++]

Why Use LinkedHashSet?

Suppose an application needs:

  • Unique elements
  • The order in which elements were added

A HashSet gives uniqueness but does not guarantee iteration order.

A LinkedHashSet provides both:

Unique elements + Insertion order

Main Characteristics

LinkedHashSet:

  • Implements Set.
  • Does not allow duplicates.
  • Maintains insertion order during iteration.
  • Allows one null element.
  • Uses hashing for element lookup.
  • Generally has slightly more memory overhead than HashSet because it maintains linked ordering information.

Creating LinkedHashSet

LinkedHashSet<String> names = new LinkedHashSet<>();

Or:

Set<String> names = new LinkedHashSet<>();

Adding Elements

names.add("Amit");
names.add("Rahul");
names.add("Priya");

Duplicate Elements

names.add("Amit");
names.add("Rahul");

These duplicates are ignored.

For example:

LinkedHashSet<String> names = new LinkedHashSet<>();
 
names.add("Amit");
names.add("Rahul");
names.add("Amit");
 
System.out.println(names);

Output

[Amit, Rahul]

Maintaining Insertion Order

Consider:

LinkedHashSet<String> languages = new LinkedHashSet<>();
 
languages.add("Java");
languages.add("Python");
languages.add("C++");
languages.add("Go");

Iteration follows the insertion order:

Java
Python
C++
Go

This is the main difference from HashSet.

Removing an Element

languages.remove("Python");

The remaining elements retain their relative insertion order.

Checking an Element

languages.contains("Java");

Getting the Size

languages.size();

Iterating Through LinkedHashSet

for (String language : languages) {
    System.out.println(language);
}

The elements are returned according to insertion order.

Allowing Null

LinkedHashSet allows one null element.

LinkedHashSet<String> names = new LinkedHashSet<>();
 
names.add("Amit");
names.add(null);
names.add(null);
 
System.out.println(names);

Only one null is stored.

Example: Removing Duplicate Values While Preserving Order

This is one practical use of LinkedHashSet.

Suppose we have:

String[] languages = {
    "Java",
    "Python",
    "Java",
    "C++",
    "Python",
    "JavaScript"
};

We want to remove duplicates but preserve the original order.

import java.util.LinkedHashSet;
import java.util.Set;
 
public class HelloWorld {
    public static void main(String[] args) {
 
        String[] languages = {
            "Java",
            "Python",
            "Java",
            "C++",
            "Python",
            "JavaScript"
        };
 
        Set<String> uniqueLanguages =
                new LinkedHashSet<>();
 
        for (String language : languages) {
            uniqueLanguages.add(language);
        }
 
        System.out.println(uniqueLanguages);
    }
}

Output

[Java, Python, C++, JavaScript]

The duplicates are removed while the first occurrence order is preserved.

LinkedHashSet vs HashSet

FeatureHashSetLinkedHashSet
Duplicate elementsNoNo
Insertion orderNot guaranteedMaintained
Hash-basedYesYes
NullOne allowedOne allowed
Memory overheadGenerally lowerGenerally higher
Typical useUnique valuesUnique values + insertion order

LinkedHashSet vs TreeSet

FeatureLinkedHashSetTreeSet
DuplicatesNoNo
OrderingInsertion orderSorted order
SortingNoYes
Hash-basedYesNo
NullOne allowedGenerally not allowed with natural ordering
Typical usePreserve insertion orderMaintain sorted elements

When Should You Use LinkedHashSet?

Use LinkedHashSet when:

  • Duplicate values must be removed.
  • Insertion order must be preserved.
  • You want Set semantics with predictable iteration order.

For example:

  • Removing duplicate search terms while preserving their first appearance.
  • Maintaining unique menu items in the order they were added.
  • Removing duplicates from imported data while preserving source order.

Important Points LinkedHashSet implements Set. Duplicate elements are not stored. Insertion order is maintained during iteration. It is based on hashing and linked ordering information. One null element is permitted. It generally uses more memory than HashSet because ordering information is maintained.

Key Takeaway

LinkedHashSet is useful when you need unique elements while preserving insertion order. It is a good choice when both uniqueness and predictable iteration order are important.

See you in next page....🙂

← HashSetTreeSet →
← HashSetTreeSet →