lambda的⽇常使⽤(多条件分组,最⼤值,最⼩值等)
1,多条件分组;对list中的实体的多个属性进⾏分组;需要注意的是:集合的对象元素需要重写hashCode和equals,才会起作⽤,并且提供⼀个有参构造;
Map<B2BSettlement, List<B2BSettlement>> b2BSettlementListMap = b2bSettlementDTOs.stream().collect(groupingBy((b2bSettlement->
new ServiceSellerId(),ServiceBuyerId(),))));
另⼀种写法:
HashMap<B2BSettlement, List<B2BSettlement>> map = b2bSettlementDTOs.stream()
.collect(groupingBy(b -> new ServiceSellerId(),
lua字符串转数组
HashMap::new, toList()));
⼀个属性的分组:
Map<BigDecimal, List<B2BSettlement>> b2BSettlementListMap
= b2bSettlementDTOs.stream().collect(groupingBy(B2BSettlement::getTaxRate));
2,对集合中的某个⾦额进⾏加总;
BigDecimal amount = combatTeamEntities.stream().map(CombatTeamEntity::getActivityAmountOne).reduce(BigDecimal::add).get();
3,对⼀个map的key,value进⾏操作;
b2bSettlementDTOs = Set().stream().map(e -> {
BigDecimal taxRate = e.getKey();
List<B2BSettlement> settlements = e.getValue();
SettlementRules(), taxRate, settlements);
}).List());
4,集合中最⼤⽇期,最⼩⽇期
最⼤⽇期
Date maxDate = settlements.stream().filter(Objects::nonNull)
.max(Comparatorparing(B2BSettlement::getConfirmDate)).get().getConfirmDate();
最⼩⽇期
Date minDate = settlements.stream().filter(Objects::nonNull)
.
min(Comparatorparing(B2BSettlement::getConfirmDate)).get().getConfirmDate();
5,bigdecimal⼤于0
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论