java8 stream 使用示例

把对象list里面的某个属性拿出来组成新的list
List<Long> orderIdList = steveList.stream().map(SteveInfoDTO::getOrderId).collect(Collectors.toList());


把list中的元素进行方法转化组成新的list
List<String> list = propertyList.stream().map(this::transferPropFromCargoToSteve).collect(Collectors.toList());

把对象list变成对象map
Map<Long, SteveInfoDTO> steveMap = steveList.stream().collect(Collectors.toMap(StevedoreDTO::getOrderId, steveInfoDTO -> steveInfoDTO));

将timeList流首先过滤时间不为空的,将需要比较的值转出map然后去重,最后取出最大值/最小值
Date min = timeList.stream().filter(o -> o.getTime() != null).map(TimeTest::getTime).distinct().min(DateUtils::compareDate).get();

字符串array转成double类型然后取最小值
Double minLen = Arrays.stream(truckLength.split(",")).map(e -> Double.parseDouble(e)).distinct().min(Double::compareTo).get();
sonarLint 提示 Call "Optional#isPresent()" before accessing the value
改为 Double minLen = Arrays.stream(truckLength.split(",")).map(e -> Double.parseDouble(e)).distinct().min(Double::compareTo).orElse(0D);

过滤掉id列表中的null值或者<=0的
cargoIdList.stream().filter(e -> e != null && e > 0).collect(Collectors.toList());

List<String> provinceIdList 变成 Set<Integer>
provinceIdList.stream().map(Integer::parseInt).collect(Collectors.toSet());

看看是否有名字叫张三的
if (list.stream().anyMatch(e-> Objects.equals(e.getName(), "张三")))

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值