java 对比两个list 去重
时间: 2023-11-23 09:56:31 浏览: 187
Java数组去重 集合List去重的常用方法总结
以下是Java中对比两个List去重的方法:
1.使用Java 8 Stream API进行去重
```java
List<String> list1 = new ArrayList<>(Arrays.asList("a", "b", "c"));
List<String> list2 = new ArrayList<>(Arrays.asList("b", "c", "d"));
List<String> result = Stream.concat(list1.stream(), list2.stream())
.distinct()
.collect(Collectors.toList());
System.out.println(result); // 输出:[a, b, c, d]
```
2.使用Java中的Set集合进行去重
```java
List<String> list1 = new ArrayList<>(Arrays.asList("a", "b", "c"));
List<String> list2 = new ArrayList<>(Arrays.asList("b", "c", "d"));
Set<String> set = new HashSet<>(list1);
set.addAll(list2);
List<String> result = new ArrayList<>(set);
System.out.println(result); // 输出:[a, b, c, d]
```
阅读全文