Java LinkedHashSet
The LinkedHashSet class is a part of the Java Collections Framework and
is used to store a collection of unique elements while preserving the
order in which they were inserted. It is available in the java.util
package and implements the Set interface.
Unlike HashSet, which does not guarantee any order, LinkedHashSet
maintains the insertion order of elements. This makes it useful when you
want both uniqueness and predictable iteration order.
Because it tracks insertion order internally, LinkedHashSet is slightly
slower than HashSet but provides better control over how elements are
stored and displayed.
Key Features of LinkedHashSet
LinkedHashSet combines the advantages of HashSet and LinkedList-like
ordering.
Unique Elements
Just like other Set implementations, duplicate elements are not
allowed.
Maintains Insertion Order
Elements are returned in the same order they were added.
No Index-Based Access
LinkedHashSet does not support accessing elements using
indexes.
Moderate Performance
It is slightly slower than HashSet because it maintains order
internally.
Creating a LinkedHashSet in Java
To use LinkedHashSet, you first need to import the class from the
java.util package and then create an object of the class.
Example: Creating a LinkedHashSet
import java.util.LinkedHashSet;
public class LanguageSetExample {
public static void main(String[] args) {
LinkedHashSet<String> languages = new
LinkedHashSet<>();
languages.add("Java");
languages.add("Python");
languages.add("C++");
languages.add("JavaScript");
System.out.println("Programming Languages: "
+ languages);
}
}
Output
The elements appear in the order they were inserted.
Adding Elements to a LinkedHashSet
You can add elements using the add() method. If a duplicate element is
added, it will be ignored automatically.
Example: Adding Device Names
import java.util.LinkedHashSet;
public class DeviceSetExample {
public static void main(String[] args) {
LinkedHashSet<String> devices = new
LinkedHashSet<>();
devices.add("Laptop");
devices.add("Tablet");
devices.add("Smartphone");
devices.add("Laptop"); // Duplicate
element
System.out.println("Devices: " +
devices);
}
}
Important Note
Even though "Laptop" is added twice, it appears only once because
LinkedHashSet stores
unique elements only.
Checking if an Element Exists
The contains() method checks whether an element exists in the
set.
Example
if(devices.contains("Tablet")) {
System.out.println("Tablet is available in the
set.");
}
This method is useful for fast membership checking.
Removing Elements from a LinkedHashSet
To remove an element from the set, use the remove() method.
Example
devices.remove("Smartphone");
To remove all elements from the set, use the clear() method.
devices.clear();
Finding the Size of a LinkedHashSet
The size() method returns the total number of unique elements in the
set.
Example
System.out.println("Total devices: " + devices.size());
Duplicate elements are not counted.
Iterating Through a LinkedHashSet
You can loop through the elements of a LinkedHashSet using a for-each
loop.
Example: Displaying Courses
import java.util.LinkedHashSet;
public class CourseIteratorExample {
public static void main(String[] args) {
LinkedHashSet<String> courses = new
LinkedHashSet<>();
courses.add("Artificial
Intelligence");
courses.add("Machine Learning");
courses.add("Cyber Security");
for(String course : courses) {
System.out.println(course);
}
}
}
The elements will be printed in the same order they were inserted.
HashSet vs LinkedHashSet
Both classes implement the Set interface, but they behave differently in
terms of ordering.
Table
Feature HashSet LinkedHashSet
Ordering No guaranteed order Maintains insertion order
Duplicate Elements Not allowed Not allowed
Performance Faster Slightly slower
Use Case Fast unique storage Unique elements
with
predictable order
Tip:
Use HashSet when performance and uniqueness are your priorities. Use
LinkedHashSet when you need unique elements while maintaining insertion
order.
Using the var Keyword (Java 10+)
Starting from Java 10, you can use the var keyword to declare variables
without repeating the type.
The compiler automatically determines the variable type based on the
assigned value.
Example
var frameworks = new LinkedHashSet<String>();
frameworks.add("Spring");
frameworks.add("Hibernate");
frameworks.add("Struts");
While var reduces code length, many developers still prefer using
explicit type declarations for readability.
Using the Set Interface with LinkedHashSet
In many Java applications, variables are declared using the Set
interface instead of the specific implementation.
Example
import java.util.Set;
import java.util.LinkedHashSet;
public class ToolSetExample {
public static void main(String[] args) {
Set<String> tools = new
LinkedHashSet<>();
tools.add("Git");
tools.add("Docker");
tools.add("Kubernetes");
System.out.println(tools);
}
}
This approach allows developers to change the implementation later
without modifying other parts of the program.