欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java stream,

来源: javaer 分享于  点击 37115 次 点评:225

java stream,


list<string>分组求和

Map<String, Map<String,Long>> mapCount = new LinkedHashMap<>();
for (Map.Entry<String, List<IndexOutProjectInfo>> entry : infoGroupMap.entrySet()) {     mapCount.put(entry.getKey(),entry.getValue().stream().collect(Collectors.groupingBy(IndexOutProjectInfo::getNd,Collectors.counting())));
}

 

list取字段放入list<string>

List<String> enames = list.stream().map(e -> e.getNd()).collect(Collectors.toList());

map升排序:

Map<String, String> sortMap = new LinkedHashMap<>();
year.entrySet().stream().sorted(Map.Entry.<String, String>comparingByValue().reversed()).forEachOrdered(e -> sortMap.put(e.getKey(), e.getValue()));

map降序

Map<String, String> sortMap = new LinkedHashMap<>(); year.entrySet().stream().sorted(Map.Entry.<String, String>comparingByValue()). forEachOrdered(e -> sortMap.put(e.getKey(), e.getValue()));

list按字段去重

List<IndexOutProjectInfo> unique = list.stream().collect(
        Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(IndexOutProjectInfo::getNd))), ArrayList::new)
);

list转map
Map<String,String> map = list.stream().collect(Collectors.groupingBy(IndexOutProjectInfo::getTypeIndex,IndexOutProjectInfo::getTypeName));

如果有重复值,用这种方式

Map<String,String> map = list.stream().collect(Collectors.toMap(IndexOutProjectInfo::getTypeIndex,IndexOutProjectInfo::getTypeName,(entity1,entity2) -> entity1));

list按字段分组

Map<String, List<IndexOutProjectInfo>> infoGroupMap = list.stream().collect(Collectors.groupingBy(IndexOutProjectInfo::getTypeIndex));

相关文章

    暂无相关文章
相关栏目:

用户点评