- 元数据
List<String> list1 = Arrays.asList("A", "B", "C", "D", "D");
List<String> tmp1 = new ArrayList<>(list1);
List<String> list2 = Arrays.asList( "C", "D", "E", "F", "E");
List<String> tmp2 = new ArrayList<>(list2);
- 删除2中与1相同的数据
tmp1.removeAll(tmp2);
// [A, B]
- 2与1共同的数据
tmp1.retainAll(tmp1);
// [C, D, D]
// Stream方式
List<String> result = list1.stream()
.filter(list2::contains)
.collect(Collectors.toList());
System.out.println(result);
// [C, D, D]
// 去掉重复
Set<String> result = list1.stream()
.filter(s -> list2.contains(s))
.collect(Collectors.toSet());
System.out.println(result);
// [C, D]
1 中特有部分
List<String> result = list1.stream()
.filter(s -> !list2.contains(s))
.collect(Collectors.toList());
System.out.println(result);
// [A, B]