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));

猜你喜欢

转载自blog.csdn.net/weixin_38863607/article/details/132339546