In this post, weβll walk through multiple ways to remove duplicates from a Java List, using:
- β Core Java (pre-Java 8)
- π Java 8+ Streams
- π LinkedHashSet for preserving order
- π§ Google Guava
Depending on your use case and preference, decide which approach is the best fit.
1. Set approach
The simpler and easier way to remove duplicates is to convert the list into a Set, since sets donβt allow duplicates.
import java.util.*;
public class RemoveDuplicate {
public static void main(String[] args) {
List<String> dataList = Arrays.asList("Apple", "Car", "Cake", "Car", "Bus","Apple");
Set<String> uniqueItems = new HashSet<>(dataList);
System.out.println(uniqueItems);
}
}
Code language: JavaScript (javascript)
If you need to preserve the order of elements, then use LinkedHashSet instead of HashSet.
Using LinkedHashSet
import java.util.*;
public class RemoveDuplicatesPreserveOrder {
public static void main(String[] args) {
List<String> dataList = Arrays.asList("Apple", "Car", "Cake", "Car", "Bus","Apple");
List<String> unique = new ArrayList<>(new LinkedHashSet<>(dataList));
System.out.println(unique);
}
}
Code language: JavaScript (javascript)
β Best for: Removing duplicates while preserving insertion order.
2. Using Java 8+ Streams
We can write clean and functional programming style code to remove duplicates from the list. Let us do that. We will use the distinct() method to get unique values. My preferred way is this approach. I find this simpler, cleaner, and easier to use.
distinct() – filters out duplicates while maintaining order.
import java.util.*;
import java.util.stream.Collectors;
public class DuplicateRemover {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5);
List<Integer> unique = numbers.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(unique);
}
}
Code language: JavaScript (javascript)
3 . Using Google Guava
Guava offers a Sets.newLinkedHashSet() utility that makes the code more expressive.
import com.google.common.collect.Sets;
import java.util.*;
public class DuplicateRemoverGuava {
public static void main(String[] args) {
List<String> colors = Arrays.asList("red", "blue", "red", "green", "blue");
List<String> uniqueColors = new ArrayList<>(Sets.newLinkedHashSet(colors));
System.out.println(uniqueColors);
}
}
Code language: JavaScript (javascript)
Comparison
Approach | Order Keeping | Comments |
---|---|---|
HashSet | No β | Simple , Unordered deduplication |
LinkedHashSet | Yes β | One line of code. Needs external dependency |
Stream.distinct() | Yes β | Cleaner, functional style, elegant |
Guava + LinkedHashSet | Yes β | One line code. Needs external dependency |
Conclusion
In this post, we have demonstrated different approaches to remove duplicates from a list using plain Java, Google Guava, and Java 8 Streams.
Leave a Reply