Backend Development 5 min read

Three Ways to Merge Two Lists and Remove Duplicates in Java

This article demonstrates three practical techniques—using a HashSet, leveraging Java 8 Stream API, and applying Map's merge functionality—to combine two List collections while eliminating duplicate elements, complete with full Java code examples and output illustration.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Three Ways to Merge Two Lists and Remove Duplicates in Java

When you need to merge two List<Integer> objects in Java and ensure the result contains no duplicate values, there are several concise approaches.

1. Using HashSet – A HashSet automatically rejects duplicate entries, so adding all elements from both lists to a set yields a deduplicated collection, which can then be converted back to a list.

package com.sample.algo.list;
import java.util.*;
// 利用Hashset元素不重复的特性
public class MergeWithHash {
public static void main(String[] args) {
// 这里要记得new一个ArrayList
List
list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
List
list2 = new ArrayList<>(Arrays.asList(4, 5, 6));
Set
set = new HashSet<>();
for (Integer element : list1) {
set.add(element);
}
for (Integer element : list2) {
set.add(element);
}
List
result = new ArrayList<>(new HashSet<>(set));
System.out.println("去重后的List:" + result);
}
}

2. Using Stream – Java 8’s Stream API provides a fluent way to concatenate two streams and apply distinct() to filter out duplicates before collecting the result into a list.

package com.sample.algo.list;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// 基于Stream来合并并去重两个List
public class MergeWithStream {
public static void main(String[] args) {
List
list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
List
list2 = new ArrayList<>(Arrays.asList(4, 5, 6));
// 把List转成Stream
Stream
stream1 = list1.stream();
Stream
stream2 = list2.stream();
// 合并List并去重
List
result = Stream.concat(stream1, stream2)
.distinct().collect(Collectors.toList());
System.out.println("去重后的List:" + result);
}
}

3. Using Map.merge – By converting the combined list into a map where the key is the element value, you can specify a merge function to resolve duplicate keys, then extract the key set as the deduplicated list.

package com.sample.algo.list;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
// 使用mergeFunction来进行合并
public class MergeWithMap {
public static void main(String[] args) {
List
list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
List
list2 = new ArrayList<>(Arrays.asList(4, 5, 6));
// 合并成一个list
List
listAll = new ArrayList<>(list1);
listAll.addAll(list2);
// 处理相同key的情况,也就是取其中一个
Map
map = listAll.stream()
.collect(Collectors.toMap(Integer::intValue, Function.identity(), (first, second) -> second));
List
result = new ArrayList<>(map.keySet());
System.out.println("去重后的List:" + result);
}
}

Running any of the above programs prints a list such as [1, 2, 3, 4, 5, 6] , confirming that duplicates have been successfully removed.

JavamaplistStreamHashSetDuplicateRemoval
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.