Learn to remove duplicate elements from a List in Java using Collection.removeIf(), LinkedHashSet and Stream APIs.
1. Using Collection.removeIf()
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 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]
2. Using Stream.distinct()
We can use the Java 8 Stream.distinct()
method which returns a stream consisting of the distinct elements compared by 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]
3. Using LinkedHashSet
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 arraylist in Java.
Happy Learning !!
Comments