🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #107

Java LinkedHashSet


Java LinkedHashSet

A LinkedHashSet is a collection that stores unique elements and remembers the order they were added.

It is part of the java.util package and implements the Set interface.

Tip: Use LinkedHashSet when you want a set that does not allow duplicates and keeps the original insertion order.


Create a LinkedHashSet

Example

import java.util.LinkedHashSet; // Import the LinkedHashSet class

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

Now you can use methods like add(), contains(), and remove() to manage your collection.


Add Elements

To add elements to a LinkedHashSet, use the add() method:

Example

import java.util.LinkedHashSet;

public class Main {
  public static void main(String[] args) {
    LinkedHashSet<String> cars = new LinkedHashSet<>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("BMW");  // Duplicate
    cars.add("Mazda");

    System.out.println(cars);
  }
}

Note: Output: The elements will appear in the order they were added (e.g., [Volvo, BMW, Ford, Mazda]). Note: Duplicates like "BMW" are ignored.


Check if an Element Exists

Use contains() to check for an element:

Example

cars.contains("Mazda");

Remove an Element

Use remove() to remove an element:

Example

cars.remove("Volvo");

Remove All Elements

Use clear() to remove all elements:

Example

cars.clear();

LinkedHashSet Size

Use size() to count how many unique elements are in the set:

Example

cars.size();

Note: Duplicate values are not counted - only unique elements are included in the size.


Loop Through a LinkedHashSet

Loop through the elements of a LinkedHashSet with a for-each loop:

Example

LinkedHashSet<String> cars = new LinkedHashSet<>();
// Add elements...

for (String car : cars) {
  System.out.println(car);
}

HashSet vs LinkedHashSet

Feature HashSet LinkedHashSet
Order No guaranteed order Insertion order preserved
Duplicates Not allowed Not allowed
Performance Faster Slightly slower (due to order tracking)

Tip: Use HashSet when you only care about uniqueness and speed. Use LinkedHashSet when order matters.


The var Keyword

From Java 10, you can use the var keyword to declare a LinkedHashSet variable without writing the type twice. The compiler figures out the type from the value you assign.

This makes code shorter, but many developers still use the full type for clarity. Since var is valid Java, you may see it in other code, so it's good to know that it exists:

Example

// Without var
LinkedHashSet<String> cars = new LinkedHashSet<String>();

// With var
var cars = new LinkedHashSet<String>();

The Set Interface

Note: Sometimes you will see both Set and LinkedHashSet in Java code, like this:

import java.util.Set;
import java.util.LinkedHashSet;

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

This means the variable (cars) is declared as a Set (the interface), but it stores a LinkedHashSet object (the actual set). Since LinkedHashSet implements the Set interface, this is possible.

It works the same way, but some developers prefer this style because it gives them more flexibility to change the type later.

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Java LinkedHashSet – FAQs

Quick answers about learning Java LinkedHashSet in Java.

This free note from CodingNow 2.0 explains Java LinkedHashSet in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Java LinkedHashSet, is 100% free with no signup required.
With focused practice, most students grasp Java LinkedHashSet in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now