5 Simple Ways to Remove Duplicates from a Java ArrayList
This article demonstrates five distinct techniques for eliminating duplicate entries from a Java ArrayList, including using LinkedHashSet, Java 8 streams, HashSet with order preservation, manual contains checks, and nested loops, each accompanied by complete code examples and output results.
Five Different Methods to Remove Duplicates from a Java ArrayList
1. Use LinkedHashSet
LinkedHashSet is an optimal way to delete duplicate data while preserving insertion order.
Removes duplicate data
Maintains the order of elements
Java example using LinkedHashSet:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
System.out.println(numbersList);
LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(numbersList);
ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet);
System.out.println(listWithoutDuplicates);
}
}Output:
[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]2. Use Java 8 Stream distinct()
Java 8 streams provide the distinct() method, which returns a stream of unique elements based on equals().
Collect the result back to a list:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
System.out.println(numbersList);
List<Integer> listWithoutDuplicates = numbersList.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(listWithoutDuplicates);
}
}Output:
[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]3. Use HashSet (order not guaranteed)
Insert elements into a HashSet to filter duplicates, then rebuild the list.
private static void removeDuplicate(List<String> list) {
HashSet<String> set = new HashSet<>(list.size());
List<String> result = new ArrayList<>(list.size());
for (String str : list) {
if (set.add(str)) {
result.add(str);
}
}
list.clear();
list.addAll(result);
}4. Use List.contains() in a loop
private static void removeDuplicate(List<String> list) {
List<String> result = new ArrayList<>(list.size());
for (String str : list) {
if (!result.contains(str)) {
result.add(str);
}
}
list.clear();
list.addAll(result);
}5. Double for‑loop removal
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if (i != j && list.get(i) == list.get(j)) {
list.remove(list.get(j));
}
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
