Various Ways to Remove Duplicates from a List in Java Using Set Implementations and Streams
This article demonstrates six different techniques for eliminating duplicate elements from a Java List, including HashSet, TreeSet, LinkedHashSet, iterator-based removal, Stream distinct, and manual contains checks, each with complete code examples and output illustrations.
Java provides several built‑in collection types and APIs that can be used to remove duplicate elements from a List<Integer>. The following sections illustrate six common approaches with full source code and sample output.
1. HashSet Deduplication
Since HashSet inherently disallows duplicate entries, converting a list to a HashSet removes duplicates instantly.
public class ListDistinctExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>() {{
add(1);add(3);add(5);add(2);add(1);
add(3);add(7);add(2);
}};
System.out.println("原集合:" + list);
method_2(list);
}
/**
* 使用 HashSet 去重
* @param list
*/
public static void method_2(List<Integer> list) {
HashSet<Integer> set = new HashSet<>(list);
System.out.println("去重集合:" + set);
}
}2. TreeSet Deduplication
TreeSetalso eliminates duplicates while optionally providing sorted order.
public class ListDistinctExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>() {{
add(1);add(3);add(5);add(2);add(1);
add(3);add(7);add(2);
}};
System.out.println("原集合:" + list);
method_4(list);
}
/**
* 使用 TreeSet 去重(无序)
* @param list
*/
public static void method_4(List<Integer> list) {
TreeSet<Integer> set = new TreeSet<>(list);
System.out.println("去重集合:" + set);
}
}3. LinkedHashSet Deduplication
LinkedHashSetremoves duplicates while preserving the original insertion order.
public class ListDistinctExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>() {{
add(1);add(3);add(5);add(2);add(1);
add(3);add(7);add(2);
}};
System.out.println("原集合:" + list);
method_3(list);
}
/**
* 使用 LinkedHashSet 去重
* @param list
*/
public static void method_3(List<Integer> list) {
LinkedHashSet<Integer> set = new LinkedHashSet<>(list);
System.out.println("去重集合:" + set);
}
}4. Iterator‑Based Deduplication
This method iterates through the list, removing an element when a duplicate is detected using the iterator’s remove() method.
public class ListDistinctExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>() {{
add(1);add(3);add(5);add(2);add(1);
add(3);add(7);add(2);
}};
System.out.println("原集合:" + list);
method_1(list);
}
/**
* 使用迭代器去重
* @param list
*/
public static void method_1(List<Integer> list) {
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
// 获取循环的值
Integer item = iterator.next();
// 如果存在两个相同的值
if (list.indexOf(item) != list.lastIndexOf(item)) {
// 移除最后那个相同的值
iterator.remove();
}
}
System.out.println("去重集合:" + list);
}
}5. Stream Distinct Deduplication
Java 8’s Stream API provides a concise distinct() operation to filter duplicates.
public class ListDistinctExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>() {{
add(1);add(3);add(5);add(2);add(1);
add(3);add(7);add(2);
}};
System.out.println("原集合:" + list);
method_5(list);
}
/**
* 使用 Stream 去重
* @param list
*/
public static void method_5(List<Integer> list) {
list = list.stream().distinct().collect(Collectors.toList());
System.out.println("去重集合:" + list);
}
}6. Contains‑Check Manual Deduplication
A new list is built by iterating over the original list and adding elements only if they are not already present in the new list.
public class ListDistinctExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>() {{
add(1);add(3);add(5);add(2);add(1);
add(3);add(7);add(2);
}};
System.out.println("原集合:" + list);
method(list);
}
/**
* 自定义去重
* @param list
*/
public static void method(List<Integer> list) {
// 新集合
List<Integer> newList = new ArrayList<>(list.size());
list.forEach(i -> {
if (!newList.contains(i)) { // 如果新集合中不存在则插入
newList.add(i);
}
});
System.out.println("去重集合:" + newList);
}
}Each snippet prints the original list followed by the deduplicated result, demonstrating how different Set implementations and Java APIs can be leveraged for duplicate removal.
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.
