Java remove duplicates from list
In this post, you will learn how to remove duplicates from a list using the Java programming language.
List in Java
A list is an ordered collection. It contains the index-based methods to insert, update, delete, and search the elements. We can access elements by their index and also search for elements in the list. A list can contain duplicate values and null elements. List interface gives positional access and insertion of elements and preserves the insertion order. The list interface extends Collection and declares the behaviour of a collection that stores a sequence of elements.
List Interface declarationpublic interface List<E> extends Collection<E>
Remove Duplicate Values From List
In the given Java program, first we create a list with duplicate values and create another empty list. We traverse through the first list and store the first appearance of each element into the second list using the contains() method. The second list does not contain any duplicate elements.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class RemoveDuplicateFromList {
public static void main(String[] args) {
// Original List with duplicate values
List<String> xlist = Arrays.asList("Apple","Orange","Kiwi","Apple","Kiwi");
System.out.println("List with duplicates: "+ xlist);
// Remove duplicates using without stream
List<String> newList = new ArrayList<>();
for(String ele : xlist) {
if(!newList.contains(ele)) {
newList.add(ele);
}
}
System.out.println("List with duplicates removed: "+ newList);
}
}
Java remove duplicate values from List with Stream API
Java provides a new additional package in Java 8 called java.util.stream. We can use the distinct() method from the Stream API to remove the duplicates from an arraylist. This method returns a stream consisting of the distinct elements compared by the object's equals() method.
In the given Java program, first we get a List with duplicate values. Next, we create a new list from this list by using the Stream().distinct() method, which returns a distinct object stream. Atlast, we convert this object stream into a list.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveDuplicateFromList {
public static void main(String[] args) {
// Original List with duplicate values
List<String> xlist = Arrays.asList("Potato","Onion","Potato","Carrot","Carrot");
System.out.println("List with duplicates: "+ xlist);
// Remove duplicates using with stream
List<String> newList =
xlist
.stream()
.distinct()
.collect(Collectors.toList());
System.out.println("List with duplicates removed: "+ newList);
}
}
Output of the above code:
List with duplicates: [Potato, Onion, Potato, Carrot, Carrot]
List with duplicates removed: [Potato, Onion, Carrot]
Related Articles
Java string split multiple delimitersEnum with values in Java
Convert array to list in Java
Java random number between 1 and 100
Calculating percentage in Java
Multiplication table program in Java
Java dialogue box
Fibonacci series using recursion in Java
Java sum of array
Circular prime in Java
Vowel and Consonant program in Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Java program to check leap year
Java program to find factorial of a number