Five Ways to Remove Duplicates from a Java ArrayList
This article presents five distinct methods for eliminating duplicate elements from a Java ArrayList, including using LinkedHashSet, Java 8 streams, HashSet with order preservation, manual iteration with contains checks, and nested loops, each accompanied by complete code examples and output results.
The following introduces five different methods to remove duplicate data from a Java ArrayList.
1. Remove duplicates from an ArrayList using LinkedHashSet
LinkedHashSet is an optimal way to delete duplicate data while preserving insertion order. The example creates a list of integers with repeated values, adds it to a LinkedHashSet, and converts the set 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. Remove duplicates using Java 8 Stream API
The Stream API’s distinct() method returns a stream of unique elements based on equals(). The distinct stream is 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. Remove duplicates using HashSet while preserving order manually
Because HashSet does not guarantee order, we use it only as a duplicate‑check while building a new list that retains 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. Remove duplicates by iterating with contains and rebuilding the list
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. Remove duplicates using a double‑for loop
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));
}
}
}These five approaches provide developers with options ranging from concise modern APIs to explicit manual algorithms for de‑duplicating an ArrayList in Java.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
