The list is the interface that creates an ordered collection of elements and can also contain duplicates. While working with Lists, developers often face the challenge of managing the list by removing duplicate elements from it. Removing the duplicate elements from a List in Java implements the code optimization practice and enhances the application’s performance.  In Java, there are several methods for removing duplicate elements from a Java List that are implemented in this article.

Method 1: Remove Duplicates From the List Using List.contains() 

To remove duplicates from the list, import all the necessary dependencies, classes, interfaces, and static methods from java.util packages such as List interface and ArrayList class:

import java.util.*;

The integer list “numList” is initialized with the given values using the List.of() method. The second list “numList2” is also declared to store the single occurrence of every element of numList:

List<Integer> numList = List.of(1,2,3,4,3,5,6,2,1);
List<Integer> numList2= new ArrayList<>();

The for loop initializes the “i” with 0 and executes until the value of “i” is greater than the “numList’s size”. Inside the if-statement, the “numList.get(i)” returns the value stored at the current index. The contains() method checks if the value is present in the numList2 or not. The value at the current index is added to the numList2 if not present using the add() method:

for ( int i =0 ; i <numList.size(); i++)
{
  if(!numList2.contains(numList.get(i)))
    {
numList2.add(numList.get(i));
    }
}

The given for loop traverses through the elements of the array and prints the elements using the get() method:

System.out.println("New List is ");
for ( int i =0 ; i<numList2.size();i++)
{
System.out.print(numList2.get(i) + "  ");
}

Output 

Method 2:  Remove Duplicates From the List Using the HashSet

The HashSet class in Java provides built-in functionality for storing unique elements. It implements the Set interface. The Set interface forms a collection of elements that do not contain any duplicated elements. To remove duplicates from the list using the HashSet class, the code is as follows:

First, import the HashSet class and required dependencies from the java.util package using the import statement. Inside the main() method, the “numList” of the integer data type is initialized with the specified values. Another list “numList2” of integer data type stores the unique elements returned by the HashSet class that accepts the “numList” as an argument in the constructor. The for loop 

traverses through each index of the “numList2” and prints the elements to verify that only the unique elements are stored: 

package removeDuplicatesFromList;
import java.util.*;
public class removeduplicates  // Main class {
public static void main(String[] args) //Main method to run the code {
    //Creating two lists     List<Integer> numList = List.of(1,2,3,4,3,5,6,2,1);
    List<Integer> numList2= new ArrayList<>(new HashSet<>(numList));

    System.out.println("New List is ");
    for (int i=0; i<numList2.size();i++)
    {
System.out.print(numList2.get(i) + "   ");
    }
}
}

Output 

Method 3: Remove Duplicates From the List Using the Stream Package 

Introduced in Java 8, the Stream is a subpackage of java.util package that provides various methods to work with a group of objects in the form of a stream. 

The necessary classes are imported from the java.util packages through the given import statements. Inside the main() method, the “newList” stores the unique elements returned by the stream.distinct() method and collected by the collect() method from the “numList”. The numList contains the duplicated elements. The for loop then prints the unique values in the newList using the println() and get() methods:

package removeDuplicatesFromList;
import java.util.List;
import java.util.stream.*;
public class removeduplicates //Main public class{
public static void main(String[] args) //Main code for execution {
List <Integer> numList = List.of(1,2,3,4,5,6,1,2,3,4);
List <Integer> newList = numList.stream().distinct().collect(Collectors.toList());

System.out.println("New String elements are as follows: ");
for ( int i =0 ; i<newList.size();i++)
{
System.out.print(newList.get(i) + "  ");
}
}
 
}

Output 

Method 4: Remove Duplicates From List Using Guava 

The Guava library was introduced as the core library by Google for Java to write optimized code and work with new and different features. These features include map utilities, immutable classes, hashing techniques, etc. 

To work with Guava Library, import the necessary methods and classes such as Lists and Sets. Similarly, include the Arrays and List class from the java.util package to create and manipulate Lists:

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.util.Arrays;
import java.util.List;

The DuplicatedList is initialized with the following duplicate elements. The second integer list “removeDuplicateList” will store the unique elements returned by the newHashSet class from the Sets Interface. The newHashSet class accepts the “DuplicatedList” as an argument in the constructor and the unique elements are filtered from it:

List<Integer> DuplicatedList = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
List<Integer> removeDuplicateList
= Lists.newArrayList(Sets.newHashSet(DuplicatedList));

The following “for loop” displays the elements as an output on the console when iterating the indexes of the “removeDuplicateList” :

System.out.println("The Elements of the List are");
for ( int i=0;i<removeDuplicateList.size();i++)
{
    System.out.print(removeDuplicateList.get(i) + "     ");
}

Output

This sums up all the methods from simple to advanced to remove duplicates from a List in Java. 

Conclusion 

The List class in Java is imported from the java.util package that provides the freedom of adding duplicates to it. However, this duplicated data often causes challenges for the users such as data duplications, reduced performance, increased memory consumption, etc. The duplicated elements can be removed from a Java List by using the stream.distinct() method, HashSet from set interface or List.contain() method with custom logic, etc. In this article, we have discussed and implemented various practices using Set Interface, Stream package, or built-in methods for removing duplicates from a Java List.