JDK8新特性Stream流的使用

1、从对象集合中快速得到某个元素的集合 ,例如从角色列表中获取ID集合:

List<long> idList = roleList.stream().map(CameraRole::getCameraId).collect(Collectors.toList());

2、对象集合转化为另外的对象集合,例如:

List<CameraGroupVO> collect = list.stream().map(this::entityVO).collect(Collectors.toList());

3、从对象集合中获取某个元素的字符串拼接,例如获取部门ID的字符串拼接:

String idsStr = deptList.stream().map(dept -> Func.toStr(dept.getId())).distinct().collect(Collectors.joining(","));

4、根据某个条件将对象集合过滤,例如从菜单集合中过滤得到符合条件的菜单集合:

List<Menu> collect = routes.stream().filter(x -> Func.equals(x.getCategory(), 1)).collect(Collectors.toList());

5、将对象集合转化为Map<Long ,Object>,例如将对象集合转化为key为主键,value为对象的map:

Map<Long, StrategyCustomerLabel> map = labelList.stream().collect(Collectors.toMap(StrategyCustomerLabel::getId, o -> o));

6、将对象集合按照某个规则分组为map,例如将账单集合按照用户唯一编码进行分组:

Map<String,List<FinanceCustomerTotalBill>> uniqueCodeMap = billList.stream().collect(Collectors.groupingBy(FinanceCustomerTotalBill::getUniqueCode));

7、将对象集合按照某个属性转化为map:

//将list转map 【key为1个属性,value为1个属性】  
Map<String, String> map =
	list.stream().collect(Collectors.toMap( 
		Student::getNo, 
		Student::getName,
	    (key1 , key2) -> key1  //(map的键重复不会报错,下面已经处理)
	));

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值