【Java】Java 8 Stream通过filter()方法筛选出对象List不被另一个对象List包含的元素

// 假设A和B是两个对象数组
List<Stu> A = new ArrayList<>();
List<Stu> B = new ArrayList<>();

// 使用 Stream API 进行筛选
List<Stu> collect = A.stream()
	.filter(a -> !B.stream()
		.anyMatch(b -> a.id().equals(b.id())))
			.collect(Collectors.toList());

collect.forEach(choose -> {
	...
})

你可能感兴趣的:(java,开发语言)