JavaLinkedHashSet
Java LinkedHashSet
ALinkedHashSet is a collection that storesunique elements andremembers the order they were added.
It is part of thejava.util package and implements theSet interface.
Tip: UseLinkedHashSet when you want a set that does not allow duplicatesand keeps the original insertion order.
Create a LinkedHashSet
Example
Create aLinkedHashSet object calledcars that will store strings:
import java.util.LinkedHashSet; // Import the LinkedHashSet classLinkedHashSet<String> cars = new LinkedHashSet<>();Now you can use methods likeadd(),contains(), andremove() to manage your collection.
Add Elements
To add elements to aLinkedHashSet, use theadd() 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); }}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
Usecontains() to check for an element:
Remove an Element
Useremove() to remove an element:
Remove All Elements
Useclear() to remove all elements:
LinkedHashSet Size
Usesize() to count how many unique elements are in the set:
Note: Duplicate values are not counted - only unique elements are included in the size.
Loop Through a LinkedHashSet
Loop through the elements of aLinkedHashSet with afor-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: UseHashSet when you only care about uniqueness and speed. UseLinkedHashSet when order matters.
The var Keyword
From Java 10, you can use thevar keyword to declare aLinkedHashSet 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. Sincevar is valid Java, you may see it in other code, so it's good to know that it exists:
Example
// Without varLinkedHashSet<String> cars = new LinkedHashSet<String>();// With varvar cars = new LinkedHashSet<String>();The Set Interface
Note: Sometimes you will see bothSet andLinkedHashSet 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 aSet (the interface), but it stores aLinkedHashSet object (the actual set). SinceLinkedHashSet implements theSet 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.

