In this tutorial, you will learn how to remove duplicates from ArrayList.
Example 1: Removing duplicates from ArrayList using LinkedHashSet
In the following example, we are removing the duplicate elements from ArrayList using LinkedHashSet. The steps followed in the program are:
1) Copying all the elements of ArrayList to LinkedHashSet. Why we choose LinkedHashSet? Because it removes duplicates and maintains the insertion order.
2) Emptying the ArrayList using clear() method.
3) Copying all the elements of LinkedHashSet(non-duplicate elements) to the ArrayList. Please find the source code below.
import java.util.ArrayList; import java.util.List; import java.util.LinkedHashSet; public class RemoveDuplicates { public static void main(String[] args) { /* Creating ArrayList of Strings and adding * elements to it */ List<String> al = new ArrayList<String>(); al.add("Ajay"); al.add("Becky"); al.add("Chaitanya"); al.add("Ajay"); al.add("Rock"); al.add("Becky"); // Displaying ArrayList elements System.out.println("Before:"); System.out.println("ArrayList contains: "+al); // Creating LinkedHashSet LinkedHashSet<String> lhs = new LinkedHashSet<String>(); /* Adding ArrayList elements to the LinkedHashSet * in order to remove the duplicate elements and * to preserve the insertion order. */ lhs.addAll(al); // Removing ArrayList elements al.clear(); // Adding LinkedHashSet elements to the ArrayList al.addAll(lhs); // Displaying ArrayList elements System.out.println("After:"); System.out.println("ArrayList contains: "+al); } }
Output:
Before: ArrayList contains: [Ajay, Becky, Chaitanya, Ajay, Rock, Becky] After: ArrayList contains: [Ajay, Becky, Chaitanya, Rock]
As you can see that the duplicates have been removed from the list and the insertion order is being preserved in the output.
Example 2: Removing duplicates using Iterator
In this example, we have an ArrayList that contain duplicate elements. We are iterating this ArrayList and adding the elements of it to a new list. We have placed a code inside iterating loop in such a way that if the element is already present in the new list, skip adding that element. This ensures that the new list is free from duplicate elements.
import java.util.*; public class JavaExample { public static void main(String args[]) { // Get the ArrayList with duplicate values ArrayList<Integer> arrList = new ArrayList<Integer>( Arrays.asList(10, 20, 20, 30, 30, 30, 50)); // Print the Original ArrayList System.out.println("Original ArrayList: "+ arrList); // Creating a new ArrayList to store unique elements ArrayList<Integer> newList = new ArrayList<Integer>(); // Iterating original ArrayList for (Integer element : arrList) { // Adding element to newList, if it contains the element // skip adding that element to not have duplicates if (!newList.contains(element)) { newList.add(element); } } // Print the ArrayList with duplicates removed System.out.println("ArrayList after removing duplicates: " + newList); } }
Output:
Original ArrayList: [10, 20, 20, 30, 30, 30, 50] ArrayList after removing duplicates: [10, 20, 30, 50]
Example 3: Removing duplicates using stream distinct()
In the following example, we are using the distinct() method of Stream API. This method removes the duplicates from the input stream and returns a new stream after removing duplicate elements.
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class JavaExample { public static void main(String[] args) { // Original ArrayList with duplicates ArrayList<Integer> arrList = new ArrayList<>( Arrays.asList(10, 20, 20, 30, 30, 30, 50)); // Print the Original ArrayList System.out.println("ArrayList with duplicates: "+ arrList); // Creating a newList after applying the stream distinct() method // on the original ArrayList to remove duplicates List<Integer> newList = arrList.stream().distinct() .collect(Collectors.toList()); // Printing ArrayList after removing duplicates System.out.println("ArrayList after removing duplicates: "+ newList); } }
Output:
ArrayList with duplicates: [10, 20, 20, 30, 30, 30, 50] ArrayList after removing duplicates: [10, 20, 30, 50]
Recommended Articles:
yogesh says
How to check whether the ArrayList contains un-ordered element or not with an in-build method?
eg.
Suppose i have a strings as “ABC” and “BCA”
and if i check this ans should be true!