Java Collections – LinkedHashSet Example
The LinkedHashSet is a hash table and Linked List based implementation of the Set interface. A LinkedHashSet is different than the HashSet because it maintains a linked list which allows it to maintain the order in which the elements were inserted into the set (insertion-order).
Big-O Notation
According to the Javadocs, this implementation provides constant-time O(1) performance for the basic operations (add and remove).
Creating a LinkedHashSet
This was the old-way prior to Generics.
Set employees = new LinkedHashSet(); LinkedHashSet employees = new LinkedHashSet();
Generics
If you look closely, you will notice that I am using Generics to limit the type to String in the Set. Generics add stability to your code, by having the computer detect type incompatibilities during compile-time. These runtime bugs would be more difficult to debug if left unchecked.
Set<String> employees = new LinkedHashSet<String>(); LinkedHashSet<String> employees = new LinkedHashSet<String>();
LinkedHashSet Points
- LinkedHashSet will maintain the insertion order. This will allow us to iterate through the set in the same order as the elements were inserted
- Better than HashSet in that you know the ordering sequence
- No duplicates contained in Set
- Faster implementation than that of TreeSet
- LinkedHashSet are not thread safe, you may need to wrap in Collections.synchronizedSet()
Adding elements
Adding elements to the employees Set is done by using the add(Object obj) method.
No Duplicates Allowed
When adding elements, we need not worry about duplicates as any dups added during the processing will not appear in the set.
scores.add(7); scores.add(18); scores.add(7); // duplicate scores.add(2); scores.add(64); scores.add(7); // duplicate
Auto-Boxing and Unboxing
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer and vice-versa without the need to cast. In this case, Java is performing the boxing when we put elements into the set (converting int to Integer wrapper class) and unboxing when we get() when iterating through the elements from the Set (converting Integer to int primitive).
employees.add("John"); employees.add("David"); employees.add("James"); employees.add("Danielle"); employees.add("Jeff"); employees.add("Chris"); employees.add("Mary"); employees.add("Tina");
Removing elements
Removing elements is just a matter of calling the remove(Object obj) method.
employees.remove("Jeff"); employees.remove("Mary");
Checking elements using contains
You can see if object is in set by calling the contains(Object obj) method.
employees.contains("Danielle"); employees.contains("Amaury");
Size of Collection
Returning the number of elements in a LinkedHashSet is as easy as calling the size() method.
employees.size();
Iterating through the Collection
Java 1.5 and above provides a foreach loop, which makes it much easier to iterate over the entire collection. This is my preferred way of doing it.
// Loop through the collection of employees for (String emp : employees) { System.out.println(emp); }
Iterating through the Collection with Generics
LinkedHashSet<String> employees = new LinkedHashSet<String>(); Iterator<String> iter = employees.iterator(); while (iterator.hasNext()) { String e = iter.next(); System.out.println(e); }
Full Program Listing
package com.avaldes.tutorials; import java.util.LinkedHashSet; import java.util.Set; public class LinkedHashSetExample { public static void main(String[] args) { LinkedHashSet<String> employees = new LinkedHashSet<String>(); LinkedHashSet<Integer> scores = new LinkedHashSet<Integer>(); // Populate the employee LinkedHashSet -- Auto-Boxing employees.add("John"); employees.add("David"); employees.add("James"); employees.add("Danielle"); employees.add("Jeff"); employees.add("Chris"); employees.add("Mary"); employees.add("Tina"); scores.add(12); scores.add(45); scores.add(23); scores.add(7); scores.add(87); scores.add(37); scores.add(29); scores.add(7); // duplicate scores.add(18); scores.add(6); scores.add(2); scores.add(64); scores.add(7); // duplicate System.out.println("Display All Employees -- prior to delete operation..."); System.out.println(employees + " size=" + employees.size()); // Let's remove employee Jeff & Mary System.out.println("nRemoving Jeff from employees"); employees.remove("Jeff"); System.out.println("Removing Mary from employees"); employees.remove("Mary"); System.out.println(employees + " size=" + employees.size()); // Is Danielle in this set? System.out.println("nIs Danielle in this set? " + employees.contains("Danielle")); // Is in this set? System.out.println("Is Amaury in this set? " + employees.contains("Amaury")); System.out.println("nDisplay All Employees and Scores..."); System.out.println(employees + " size=" + employees.size()); System.out.println(scores + " size=" + scores.size()); System.out.println("nDisplaying the Employees.."); for (String emp : employees) { System.out.println(emp); } } }
Output

Display All Employees -- prior to delete operation... [John, David, James, Danielle, Jeff, Chris, Mary, Tina] size=8 Removing Jeff from employees Removing Mary from employees [John, David, James, Danielle, Chris, Tina] size=6 Is Danielle in this set? true Is Amaury in this set? false Display All Employees and Scores... [John, David, James, Danielle, Chris, Tina] size=6 [12, 45, 23, 7, 87, 37, 29, 18, 6, 2, 64] size=11 Displaying the Employees.. John David James Danielle Chris Tina
Other Related Posts
Map Examples
- Hashtable Example
Simple example shows you step by step how to use Hashtable - HashMap Example
Simple example shows you step by step how to use HashMap - TreeMap Example
Simple example shows you step by step how to use TreeMap to sort a collection - EnumMap Example
Simple example shows you step by step how to use EnumMap for type-safety and speed of finite list of elements - WeakHashMap Example
Simple example shows you step by step how to use WeakHashMap - LinkedHashMap Example
Simple example shows you step by step how to use LinkedHashMap - Performance Comparison HashMap vs Hashtable vs TreeMap
Performance Comparison - Performance Comparison HashMap vs Hashtable vs TreeMap Benchmark Test
List Examples
- Stack Example
Simple example shows you step by step how to use Stack - Vector Example
Simple example shows you step by step how to use Vector - LinkedList Example
Simple example shows you step by step how to use LinkedList - ArrayList Example
Simple example shows you step by step how to use ArrayList - Performance Comparison between the four list implementations
Performance Comparison of ArrayList, LinkedList, Vector, and Stack - Performance Comparison ArrayList vs LinkedList
Performance Comparison - ArrayList vs LinkedList
Set Examples
- BitSet Example
Simple example shows you step by step how to use BitSet - EnumSet Example
Simple example shows you step by step how to use EnumSet - HashSet Example
Simple example shows you step by step how to use HashSet - TreeSet Example
Simple example shows you step by step how to use TreeSet - LinkedHashSet Example
Simple example shows you step by step how to use LinkedHashSet
Please Share Us on Social Media






Leave a Reply