Java remove duplicates from arraylist
In this post, you will learn how to remove duplicates from an arraylist using the Java programming language.
ArrayList is a part of the collection framework and is present in the java.util package. It is like an array, but there is no size limit. ArrayList is a dynamic array, and we do not have to specify the size while creating it. We can add or remove elements at any time. ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time.
ArrayList DeclarationArrayList<Type> arrayList= new ArrayList<>();
Remove duplicates from arraylist using LinkedHashSet
In the given Java program, we have used the LinkedHashSet to remove duplicate elements from an arraylist. Using this, we have added all elements of the given arraylist into the LinkedHashSet, and then we have converted the linkedhashSet to the arraylist. The result arraylist does not contain any duplicate elements.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
public class RemoveDuplicateElements
{
public static void main(String[] args)
{
// ArrayList with duplicate elements
ArrayList numList = new ArrayList<>(Arrays.asList(4, 3, 3, 5, 6, 4, 5, 8, 9, 2));
System.out.println(numList);
LinkedHashSet hashSet = new LinkedHashSet<>(numList);
ArrayList result = new ArrayList<>(hashSet);
System.out.println(result);
}
}
Output of the above code:
[4, 3, 3, 5, 6, 4, 5, 8, 9, 2]
[4, 3, 5, 6, 8, 9, 2]
Remove duplicates from arraylist using Iterator
In the given Java program, first we create an ArrayList with duplicate values and create another empty ArrayList. We traverse through the first arraylist and store the first appearance of each element into the second arraylist using the contains() method. The second arraylist does not contain any duplicate elements.
import java.util.*;
public class RemoveDuplicateElements
{
// Function to remove duplicates from an ArrayList
public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)
{
// Create a new ArrayList
ArrayList<T> newArrList = new ArrayList<T>();
// Traverse through the first list
for (T element : list) {
// If this element is not present in newList
// then add it
if (!newArrList.contains(element)) {
newArrList.add(element);
}
}
// return the new list
return newArrList;
}
// Driver code
public static void main(String args[])
{
// Get the ArrayList with duplicate values
ArrayList<Integer>
list = new ArrayList<>(
Arrays
.asList(1, 10, 1, 2, 2, 3, 3, 10, 4, 5));
// Print the Arraylist
System.out.println("ArrayList with duplicates: "
+ list);
// Remove duplicates
ArrayList<Integer>
newList = removeDuplicates(list);
System.out.println("ArrayList with duplicates removed: " + newList);
}
}
Output of the above code:
ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 3, 10, 4, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]
Remove duplicates from arraylist using Java 8 Stream.distinct()
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 an ArrayList with duplicate values. Next, we create a new list from this arraylist by using the Stream().distinct() method, which returns a distinct object stream. Atlast, we convert this object stream into an arraylist.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class RemoveDuplicateElements {
public static void main(String[] args) {
// create an arraylist from the array
ArrayList<Integer> arrList = new ArrayList<>(Arrays.asList(5, 3, 5, 2, 2, 1, 8));
System.out.println("ArrayList with duplicate elements: "+ arrList);
// create a stream from arraylist
Stream<Integer> stream = arrList.stream();
// remove duplicate elements
// by calling the distinct() of Stream
stream = stream.distinct();
// convert the stream to arraylist
arrList = (ArrayList<Integer>)stream.collect(Collectors.toList());
System.out.println("ArrayList without duplicate elements: " + arrList);
}
}
Output of the above code:
ArrayList with duplicate elements: [5, 3, 5, 2, 2, 1, 8]
ArrayList without duplicate elements: [5, 3, 2, 1, 8]
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