Five Methods to Remove Duplicates from a Java ArrayList
This article presents five distinct techniques for eliminating duplicate elements from a Java ArrayList, including using LinkedHashSet, Java 8 Stream distinct, HashSet with order preservation, manual contains checks, and a double‑for‑loop approach, each accompanied by complete code examples and output results.
The article demonstrates five different ways to delete duplicate entries from a Java ArrayList, providing both explanation and runnable code for each method.
1. Using LinkedHashSet – A LinkedHashSet removes duplicates while preserving insertion order. The example creates a list of integers, converts it to a LinkedHashSet, then back to a list.
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. Using Java 8 Stream distinct() – The Stream API’s distinct() method returns a stream of unique elements, which can be collected back into 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. Using HashSet with order check – Because a plain HashSet does not guarantee order, this method adds elements to a HashSet only if they are not already present, building a new list that keeps the original order.
private static void removeDuplicate(List<String> list) {
HashSet<String> set = new HashSet<String>(list.size());
List<String> result = new ArrayList<String>(list.size());
for (String str : list) {
if (set.add(str)) {
result.add(str);
}
}
list.clear();
list.addAll(result);
}4. Using List.contains() – Iterate through the original list and add each element to a new list only if the new list does not already contain it.
private static void removeDuplicate(List<String> list) {
List<String> result = new ArrayList<String>(list.size());
for (String str : list) {
if (!result.contains(str)) {
result.add(str);
}
}
list.clear();
list.addAll(result);
}5. Double for‑loop removal – A nested loop compares each pair of elements and removes the duplicate when found.
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));
}
}
}Each technique offers a trade‑off between simplicity, performance, and order preservation, allowing developers to choose the most suitable approach for their specific use case.
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.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
