java8 两个相同list去重,过滤相同元素
@Test
public void test12(){
// 需要过滤的集合
List<Employee> first = Arrays.asList(
new Employee(102, "李四", 79, 6666.66, Status.BUSY),
new Employee(101, "张三", 18, 9999.99, Status.FREE),
new Employee(103, "王五", 28, 3333.33, Status.VOCATION),
new Employee(104, "赵六", 8, 7777.77, Status.BUSY),
new Employee(105, "赵六", 8, 7777.77, Status.FREE),
new Employee(106, "赵六", 8, 7777.77, Status.FREE),
new Employee(107, "田七", 38, 5555.55, Status.BUSY)
);
// 参照集合
List<Employee> second = Arrays.asList(
new Employee(102, "李四", 79, 6666.66, Status.BUSY),
new Employee(104, "赵六", 8, 7777.77, Status.FREE),
new Employee(105, "田七", 38, 5555.55, Status.BUSY)
);
List<Employee> lastResult = first.stream()
.filter(add -> second.stream()
.noneMatch(all -> Objects.equals(add.getId(), all.getId())&& Objects.equals(add.getAge(), all.getAge()) ) ).collect(Collectors.toList());
lastResult.forEach(System.out::println);
//过滤条件: Objects.equals(add.getId(), all.getId())
}
本文来自博客园,作者:格,转载请注明原文链接:https://www.cnblogs.com/yuangyuan/p/16449476.html