
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Duplicate Elements from ArrayList in Java
ArrayList class in Java is a part of the Java Collections Framework and is used for storing a dynamically sized collection of elements.
It is similar to an array, but it can grow and shrink in size according to the requirements of the program.
Removing Duplicate Elements from an ArrayList
In an ArrayList, we can store duplicate elements. In this article, we will see how to remove duplicate elements from an ArrayList in Java. The following are the methods we will use to remove duplicate elements from an ArrayList:
Using removeIf() Method
The removeIf() method belongs to the Collection Interface. It accepts a Predicate object as a parameter and removes all the elements from the current collection that satisfy the given condition.
To remove duplicates from an ArrayList object, we need to create a predicate object that will iterate through each element in the current object, check if it already exists, and pass it as a parameter to the removeIf() method.
Example
In the following is the program to remove duplicate elements from an ArrayList using the removeIf() method.
import java.util.ArrayList; import java.util.List; public class RemoveDupl { public static void main(String[] args){ List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Apple"); list.add("Cherry"); list.add("Banana"); System.out.println("Original List: " + list); list.removeIf(i -> { int count = 0; for (String s : list) { if (s.equals(i)) { count++; } } return count > 1; }); System.out.println("List after removing duplicates: " + list); } }
Output
Following is the output of the above code:
Original List: [Apple, Banana, Apple, Cherry, Banana] List after removing duplicates: [Apple, Banana, Cherry]
Using Set Interface
Here, we will use a Set, because a set cannot have duplicate elements. We will create a set from the ArrayList and then convert it back to an ArrayList. This will remove all the duplicate elements from the list.
Example
In the following is the program to remove duplicate elements from an ArrayList using a Set.
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class RemoveDupl { public static void main(String[] args){ List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Apple"); list.add("Cherry"); list.add("Banana"); System.out.println("Original List: " + list); Set<String> set = new HashSet<>(list); list.clear(); list.addAll(set); System.out.println("List after removing duplicates: " + list); } }
Output
Following is the output of the above code:
Original List: [Apple, Banana, Apple, Cherry, Banana] List after removing duplicates: [Apple, Banana, Cherry]
Using Stream API
In Java Stream API, we can use the distinct() method to remove duplicate elements from a stream. We will convert the ArrayList to a stream, apply the distinct() method, and then collect the results back into an ArrayList.
Example
In the following is the code to remove duplicate elements from an ArrayList using the Stream API.
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class RemoveDupl { public static void main(String[] args){ List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Apple"); list.add("Cherry"); list.add("Banana"); System.out.println("Original List: " + list); List<String> distinctList = list.stream() .distinct() .collect(Collectors.toList()); System.out.println("List after removing duplicates: " + distinctList); } }
Output
Following is the output of the above code:
Original List: [Apple, Banana, Apple, Cherry, Banana] List after removing duplicates: [Apple, Banana, Cherry]