All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Remove Duplicates From List

Last Updated : Mar 11, 2024

Java Remove Duplicates From List

In this article we will show you the solution java remove duplicates from list, a list may contain duplicate elements, and in certain scenarios, it is desirable to eliminate these duplicates to ensure uniqueness.

We will explore two common approaches: a HashSet and the Stream API.

Step By Step Guide On Java Remove Duplicates From List :-

Method 1 - Using a HashSet

One way to remove duplicates from a list is by using a HashSet. The HashSet data structure does not allow duplicate elements, so we can take advantage of this property to filter out duplicates.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class RemoveDuplicates {
    public static <T> List<T> removeduplicates(List<T> list) {
        HashSet<T> set = new HashSet<>(list);
        return new ArrayList<>(set);
    }
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(2);
        numbers.add(4);
        numbers.add(1);
        List<Integer> uniqueNumbers = removeDuplicates(numbers);
        System.out.println("Original List: " + numbers);
        System.out.println("List with Duplicates Removed: " + uniqueNumbers);
    }
}
  1. We import the necessary classes, including ArrayList, HashSet, and List.
  2. We define a class called RemoveDuplicates and declare the removeDuplicates method that takes a generic list as a parameter.
  3. Inside the removeDuplicates method, we create a new HashSet called set and pass the original list as an argument. The HashSet automatically removes duplicate elements, leaving only the unique elements in the set.
  4. We then create a new ArrayList called uniqueList and pass the HashSet set as an argument. This creates a new list with the unique elements from the original list.
  5. Finally, we return the uniqueList.
  6. In the main method, we create an example ArrayList called numbers with some duplicate elements.
  7. We call the removeDuplicates method, passing the numbers list as an argument.
  8. The unique elements are stored in the uniqueNumbers list.
  9. We display the original list and the list with duplicates removed.

Method 2 - Using the Stream API

Another approach to remove duplicates from a list is by using the Stream API introduced in Java 8.

We can leverage the distinct() method provided by the Stream API to filter out duplicate elements.

import java.util.ArrayList;
import java.util.List;
public class RemoveDuplicates {
    public static <T> List<T> removeDuplicates(List<T> list) {
        return list.stream().distinct().toList();
    }
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(2);
        numbers.add(4);
        numbers.add(1);
        List<Integer> uniqueNumbers = removeDuplicates(numbers);
        System.out.println("Original List: " + numbers);
        System.out.println("List with Duplicates Removed: " + uniqueNumbers);
    }
}
  1. We import the necessary classes, including ArrayList and List.
  2. We define a class called RemoveDuplicates and declare the removeDuplicates method that takes a generic list as a parameter.
  3. Inside the removeDuplicates method, we use the Stream API to convert the list into a stream of elements using the stream() method.
  4. We chain the distinct() method to the stream, which filters out duplicate elements and retains only the unique ones.
  5. Finally, we convert the stream back to a list using the toList() method.
  6. In the main method, we create an example ArrayList called numbers with some duplicate elements.
  7. We call the removeDuplicates method, passing the numbers list as an argument.
  8. The unique elements are stored in the uniqueNumbers list.
  9. We display the original list and the list with duplicates removed.

Conclusion :-

we explored two approaches to remove duplicates from a list in Java.

The first approach utilized a HashSet to automatically remove duplicate elements and create a new list with unique elements.

The second approach leveraged the Stream API's distinct() method to filter out duplicates and create a new list.

Understanding these techniques is valuable for working with collections and ensuring uniqueness in datasets.

Choose the method that best suits your needs based on the requirements and characteristics of your specific application.

I hope this article on java remove duplicates from list helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪