工作中用到的Lambda笔记

有集合如下:

List<MonthFinanceIdxDetail> data = ...

1.  对象集合去重:
data = data.stream().distinct().collect(Collectors.collectors.collectingAndThen(Collectors.
toCollection(()->new TreeSet<>(Comparator.comparing(MonthFinanceIdxDetail::
getIdxCode))),ArrayList::new));

2.  把data中的指标code和值组成Map
Map<String,Double> map =data.stream().collect(Collectors.toMap(MonthFinanceIdxDetail::
getCode,MonthFinanceIdxDetail::getSum,(key1,key2)->key2));

3.  排序
data.sort((a,b)->{return b.getSum()-a.getSum();});


4.  过滤出(不是过滤掉)
data = data.stream().filter(m->topics.contains(m.getTopicCode()));

//遍历forEach

data.forEach(a->{

if(){

......

}

});

 

对数据进行分类:

5.  有对象集合:List<WarnRule> list 

//按topicCode对集合进行分组

Map<String,List<WarnRule>> collect = list.stream().collect(Collectors.groupingBy(WarnRule::getTopicCode));

Map<String,Object> countMap = Maps.newHashMap();

//然后计数每个集合有多少个

6.  collect.forEach((topicCode,warnRule)-> countMap.put(topicCode,warnRule.size()));

7.  optional

  Optional<DimensionDto> optional = dimensionList.stream().filter(dimension->"1".equals(dimension.getCode())).findFirst();

  DimensionDto root = optional.orElse(null);

8.有对象集合:List<OverviewLightDTO> list ,OverviewLightDTO有属性warnLightType,表示灯类型

  现在计算每种灯的个数

 Map<String,Long> pieMap = list.stream().collect(Collectors.groupBy(OverviewLightDTO::getWarnLightType,Collectors.counting()));

key就是灯类型,value是这个灯类型的数量

 

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值