Remove Duplicate Elements from a Java List

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

ApproachOrder KeepingComments
HashSetNo ❌Simple , Unordered deduplication
LinkedHashSetYes βœ…One line of code. Needs external dependency
Stream.distinct()Yes βœ…Cleaner, functional style, elegant
Guava + LinkedHashSetYes βœ…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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


 We break down complex topics into clear, actionable content. Built for developers, learners, and curious minds.


Socail Connect

theStemBookDev

about science, tech, engineering , programming and more