Learn to remove duplicate elements from a List in Java using Collection.removeIf(), HashSet, LinkedHashSet, and Stream APIs.
This table compares the different approaches and their advantages.
Method | Advantage | Code |
---|---|---|
HashSet | Simplest approach | HashSet<Integer> hashSet = new HashSet<>(originalList); ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet); |
LinkedHashSet | Maintains the elements ordering | LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(originalList); ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet); |
Collection.removeIf() | Modifies the original collection | Set<Integer> set = new HashSet<>(originalList.size()); originalList.removeIf(p -> !set.add(p)); |
Stream.distinct() | Opportunity to chain other operations | List<Integer> listWithoutDuplicates = originalList.stream().distinct().collect(Collectors.toList()); |
1. Using Collection.removeIf() to Remove Duplicates from Original List
The removeIf() method removes all of the elements of this collection that satisfy a specified Predicate. Each matching element is removed using Iterator.remove(). If the collection’s iterator does not support removal, then an UnsupportedOperationException will be thrown on the first matching element.
In the following code, the predicate adds the current element to a HashSet. As a HashSet does not allow duplicate items, the add() method returns false for them. All such duplicate items are removed from the List, and finally, the List contains only the unique items.
List<Integer> items = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
Set<Integer> set = new HashSet<>(items.size());
items.removeIf(p -> !set.add(p));
System.out.println(items); //[1, 2, 3, 4, 5, 6, 7, 8]
Note that this method modifies the elements in the original list, and does not create a new list.
2. Using Stream.distinct() to Remove Duplicate Elements and Get a New List
We can use the Java 8 Stream.distinct() method which returns a stream consisting of the distinct elements compared by the object’s equals() method. Finally, collect all district elements as List
using Collectors.toList().
ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
List<Integer> listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList());
System.out.println(listWithoutDuplicates); //[1, 2, 3, 4, 5, 6, 7, 8]
This method does not modify the original list and returns a new List.
3. Using LinkedHashSet to Remove Duplicates and Maintain Order
The LinkedHashSet is another good approach for removing duplicate elements in an ArrayList. LinkedHashSet does two things internally :
- Remove duplicate elements
- Maintain the order of elements added to it
In the given example, items in the ArrayList contain integers; some are duplicate numbers e.g. 1, 3, and 5. We add the list to LinkedHashSet and then get back the content back into the list. The result arraylist does not have duplicate integers.
ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(items);
ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet);
System.out.println(listWithoutDuplicates); //[1, 2, 3, 4, 5, 6, 7, 8]
Drop me your questions related to how to remove duplicate objects in the arraylist in Java.
Happy Learning !!
Comments