【Java】Stream 流常见写法总结

获取集合中某个字段值的集合

List<Long> userIdList = vipOrders.stream()
     .map(VipOrder::getUserId)
     .collect(Collectors.toList());                   

获取map<T,T> 格式数据

  • // 格式为:{1=1号,2=2号} 后面可以返回实体类functity

// 格式为:{1=1号,2=2号}  后面可以返回实体类functity
Map<Long, String> vipSkuMap = vipSkuList.stream()
   .collect(Collectors.toMap(VipSku::getId, VipSku::getSkuName));

Map<Long, VipSku> vipSkuMap = vipSkuList.stream()
   .collect(Collectors.toMap(VipSku::getId, Function.identity()));

Map<String, BackcalProgramDimension> dimensionMap = programDimensionList
    .stream()
    .collect(Collectors.toMap(k -> k.getOrganizationId() + "|" + k.getRoleId() , v->v));    

List<DropResponse> respList = list.stream()
    .map(b -> new DropResponse(b.getId(), b.getBusinessName()))
    .collect(Collectors.toList());

组成新的集合返回(BeanUtils.copyProperties)

List<VipOrderDetailResponse> list = vipOrders.stream().map(vipOrder -> {
VipOrderDetailResponse response = new VipOrderDetailResponse();
    BeanUtils.copyProperties(vipOrder, response);
    response.setCashFee(new BigDecimal(vipOrder.getCashFee()));
    response.setAmount(new BigDecimal(vipOrder.getAmount()));
    return response;
}).collect(Collectors.toList());

判断值是否在集合中

  • // 判断考核期是否存在考核期表内

// 判断考核期是否存在考核期表内
boolean isExitPeriod = periodList.stream()
    .map(Period::getPeriod)
    .collect(Collectors.toList()).contains(period);

过滤掉条件为false的,只保留为true的

  • // 最后面还可以加上 .orElse(null) 意思是:如果对象为空,则返回orElse括号里的内容。filter里面可以有多个条件使用&

Optional<CalculationConstantHistory> calculationConstantItem = calculationConstantList.stream()
    .filter(item -> item.getConstantId().equals(calculationConstant.getConstantId()))
    .findFirst();
// 最后面还可以加上 .orElse(null)  意思是:如果对象为空,则返回orElse括号里的内容。filter里面可以有多个条件使用&
// 判断上面对象是否存在
if (calculationConstantItem.isPresent()) {
    // 业务逻辑
}

list 对象属性进行去重,并排序返回新的集合

  • Collectors.collectingAndThen 的第二个参数 ArrayList::new 收集起来的集合然后调用第一个参数的去重方法,然后返回新的集合。

  • treeSet 就是用比较器来进行排序去重,如果 compareTo 返回0,说明是重复的,返回的是自己的某个属性和另一个对象的某个属性的差值,如果是负数,则往前面排,如果是正数,往后面排,是一个循环比较的过程,一旦有一个相等,就不再比较。

List<Student> studentList = new ArrayList<>();
studentList.add(new Student("111", 132774, 12, "1"));
studentList.add(new Student("123", 13556, 15, "1");
studentList.add(new Student("1146", 13165142, 16, "1"));
studentList.add(new Student("111", 132774, 14, "2"));
studentList.add(new Student("141321", 5641542, 15,"2"));
studentList.add(new Student("1454135", 2222542, 15, "2"));
List<Student> collect = studentList.stream().collect(
    Collectors.collectingAndThen(
        Collectors.toCollection(
            () -> new TreeSet<>(Comparator.comparing(Student::getStuName))),ArrayList::new));
System.out.println(collect);
System.out.println(collect.size());  // 输出结果是五条

list对象某属性值去重,并返回该值集合

  • //方案上所有去重后的组织

List<String> orgIdList = programDimensionList.stream()
    .map(BackcalProgramDimension::getOrganizationId)
    .distinct()
    .collect((Collectors.toList());

未完待续(遇到别的话,再总结)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值