Remove Duplicate Elements from a List in Java



In this article, we will learn to remove duplicate elements from a List in Java. We will be using two methods: LinkedHashSet and the Stream API. First, we'll create a list with duplicate values and use LinkedHashSet to remove them while maintaining the order. Then, we'll use the Stream API to filter out duplicates using distinct(). By the end, you'll see how both methods work to clean up the list while keeping the original order.

List in Java

The List interface in Java, part of the Collection framework, represents an ordered collection of elements that allows duplicates and provides index-based access. Common implementations include ArrayList and LinkedList.

Removing Duplicate Elements from a List

Following are the different approaches to removing duplicate elements from a List ?

Using LinkedHashSet

In this program, we use ArrayList to store elements that may include duplicates. The LinkedHashSet class is used to remove those duplicates because it implements the Set interface, which does not allow duplicates and the LinkedHashSet preserves the order of the elements as they were inserted which is why we use it here. In the end, the Demo class handles all the operations in the program.

Creating the ArrayList

ArrayList list = new ArrayList();
list.add("Jacob");
list.add("Gary");
list.add("Gary");
list.add("Harry");
list.add("Harry");
list.add("Kevin");
System.out.println("List = " + list);

An ArrayList named list is created to hold a collection of strings. We will be using .add() method: 

The .add() method is called to insert elements like "Jacob," "Gary," "Harry," and "Kevin" into the list. Duplicate elements such as "Gary" and "Harry" are intentionally added to demonstrate how duplicates can exist in an ArrayList.

Remove Duplicates Using LinkedHashSet

Set set = new LinkedHashSet(list);
System.out.println("List after removing duplicate elements = " + set);

In the above code, we are using LinkedHashSet as the LinkedHashSet doesn't allow duplicate values this effectively removes duplicates from the list. Maintains the insertion order of elements and will print the modified list after duplicates are removed.

Example

Below is the Java program demostration to remove duplicate elements from a List using LinkedHashSet ?

// Importing Classes  
import java.util.ArrayList;  
import java.util.LinkedHashSet;  
import java.util.Set;  

// Declaring the Class and Main Method  
public class Demo {  
   public static void main(String[] args) {  

      //Create and fill the List  
      ArrayList<String> list = new ArrayList<String>();  
      list.add("Jacob");  
      list.add("Gary");  
      list.add("Gary");  
      list.add("Harry");  
      list.add("Harry");  
      list.add("Kevin");  
      System.out.println("Original List (Section 3): " + list);  

      //Remove Duplicates Using LinkedHashSet  
      Set<String> set = new LinkedHashSet<String>(list);  
      System.out.println("List After Removing Duplicates (Section 4): " + set);  
   }  
}  

Output

List = [Jacob, Gary, Gary, Harry, Harry, Kevin]
List after removing duplicate elements = [Jacob, Gary, Harry, Kevin]

Using distinct() method of Stream API

In this program, we use several classes to manipulate and process the list of elements. The ArrayList class is used to create a list that can store elements, including duplicates. The Arrays class helps us quickly convert an array of values into a list using the asList() method.

We use the List interface to define the type of collection because it provides the necessary methods for working with lists, like adding and accessing elements. The Collectors class is used at the end to convert the stream back into a list, using the toList() method, which gathers the unique elements after removing duplicates.

Creating and filling the List

An ArrayList is created and filled with elements using Arrays.asList(). Duplicate elements like "Ryan" and "Mark" are intentionally added to show how duplicates are handled and the original list is printed to show the duplicates

 // Create and fill the List  
      List<String> list = new ArrayList<String>(Arrays.asList("Ryan", "Kevin", "Ryan", "Harry", "Mark", "Mark"));  
      System.out.println("Original List (Section 3): " + list);  
;

Removing Duplicates Using Stream API

We will be using Stream API to process the list and remove the duplicate elements from it. The methods that we are using here are:

We will use the stream() method which will convert the list into a stream of elements and remove duplicate values from the stream, keeping only unique ones we will use the distinct() method. To collect the unique elements back into a list we have collect(Collectors.toList()).

List<String> distinctList = list.stream()  
         .distinct()  
         .collect(Collectors.toList());  
      System.out.println("Updated List Without Duplicates (Section 4): " + distinctList); 

Example

Below is the Java program demostration to remove duplicate elements from a List using Stream API ?

//Importing the Classes  
import java.util.ArrayList;  
import java.util.Arrays;  
import java.util.List;  
import java.util.stream.Collectors;  

// Declaring the Class and Main Method  
public class Demo {  
   public static void main(String[] args) {  

      // Create and fill the List  
      List<String> list = new ArrayList<String>(Arrays.asList("Ryan", "Kevin", "Ryan", "Harry", "Mark", "Mark"));  
      System.out.println("Original List (Section 3): " + list);  

      // Remove Duplicates Using Stream API  
      List<String> distinctList = list.stream()  
         .distinct()  
         .collect(Collectors.toList());  
      System.out.println("Updated List Without Duplicates (Section 4): " + distinctList);  
   }  
}  

Output

ArrayList = [Ryan, Kevin, Ryan, Harry, Mark, Mark]
Updated ArrayList without duplicates = [Ryan, Kevin, Harry, Mark]
Updated on: 2024-11-23T03:44:22+05:30

678 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements