有集合如下:
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是这个灯类型的数量