java集合对象取差集、交集

public static void main(String[] args) {
		List<UserTest> list1 = Arrays.asList(
				new UserTest(1l,"张三",18),
				new UserTest(2l,"李四",19),
				new UserTest(3l,"王五",20),
				new UserTest(4l,"赵六",30)
		);
		List<UserTest> list2 = Arrays.asList(
				new UserTest(1l,"张三",18), new UserTest(2l,"李四",29), new UserTest(null,"田七",30)
		);
		//创建
		List<UserTest> collect1 = list2.stream().filter(s ->  Objects.nonNull(s.getId())).collect(Collectors.toList());
		//更新
		List<UserTest> collect2 = list2.stream().filter(s ->  Objects.isNull(s.getId())).collect(Collectors.toList());
		//取出传递过来集合的ID
		List<Long> collect = list2.stream().filter(s -> s.getId() != null).map(UserTest::getId).collect(Collectors.toList());
		//取出删除的数据
		List<UserTest> collect3 = list1.stream().filter(s -> !collect.contains(s.getId())).collect(Collectors.toList());
		System.out.println(collect1);
		System.out.println(collect2);
		System.out.println(collect3);
	}

	@Data
	@AllArgsConstructor
	@NoArgsConstructor
	static
	class UserTest {
		Long id ;
		String name;
		int age;
	}
[RecommendServiceServiceImpl.UserTest(id=1, name=张三, age=18), RecommendServiceServiceImpl.UserTest(id=2, name=李四, age=29)]
[RecommendServiceServiceImpl.UserTest(id=null, name=田七, age=30)]
[RecommendServiceServiceImpl.UserTest(id=3, name=王五, age=20), RecommendServiceServiceImpl.UserTest(id=4, name=赵六, age=30)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值