Remove Duplicates Using HashSet in Java: Simple Guide
To remove duplicates in Java, add elements to a
HashSet because it stores only unique values. You can convert your list or array to a HashSet and then back to a list or array to get a collection without duplicates.Syntax
The basic syntax to remove duplicates using HashSet involves creating a HashSet from a collection or array. The HashSet automatically removes duplicates because it only stores unique elements.
HashSet<Type> set = new HashSet<>(collection);- creates a set from a collection.new ArrayList<>(set);- converts the set back to a list without duplicates.
java
HashSet<Type> set = new HashSet<>(collection); List<Type> listWithoutDuplicates = new ArrayList<>(set);
Example
This example shows how to remove duplicates from a list of strings using HashSet. It prints the original list with duplicates and the new list without duplicates.
java
import java.util.*; public class RemoveDuplicates { public static void main(String[] args) { List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Alice", "Charlie", "Bob")); System.out.println("Original list: " + names); HashSet<String> set = new HashSet<>(names); List<String> uniqueNames = new ArrayList<>(set); System.out.println("List without duplicates: " + uniqueNames); } }
Output
Original list: [Alice, Bob, Alice, Charlie, Bob]
List without duplicates: [Alice, Bob, Charlie]
Common Pitfalls
One common mistake is expecting the order of elements to stay the same after removing duplicates. HashSet does not keep the order of insertion, so the output list may have elements in a different order.
To keep order, use LinkedHashSet instead.
java
import java.util.*; public class RemoveDuplicatesOrder { public static void main(String[] args) { List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Alice", "Charlie", "Bob")); System.out.println("Original list: " + names); // Using HashSet (order not guaranteed) HashSet<String> set = new HashSet<>(names); List<String> uniqueNames = new ArrayList<>(set); System.out.println("Using HashSet (order not guaranteed): " + uniqueNames); // Using LinkedHashSet (order preserved) LinkedHashSet<String> linkedSet = new LinkedHashSet<>(names); List<String> uniqueOrderedNames = new ArrayList<>(linkedSet); System.out.println("Using LinkedHashSet (order preserved): " + uniqueOrderedNames); } }
Output
Original list: [Alice, Bob, Alice, Charlie, Bob]
Using HashSet (order not guaranteed): [Alice, Bob, Charlie]
Using LinkedHashSet (order preserved): [Alice, Bob, Charlie]
Quick Reference
Summary tips for removing duplicates using HashSet in Java:
- Use
HashSetto remove duplicates quickly. - Convert your collection to a
HashSetand back to a list or array. - Order is not preserved with
HashSet; useLinkedHashSetto keep order. HashSetworks with any object type that properly implementsequals()andhashCode().
Key Takeaways
Use HashSet to remove duplicates because it stores only unique elements.
Converting a list to a HashSet and back removes duplicates efficiently.
HashSet does not preserve element order; use LinkedHashSet to keep order.
HashSet works with any objects that correctly implement equals() and hashCode().
Always test output to confirm duplicates are removed as expected.