首页 > 编程语言 > 详细

java8 Lambda Stream操作list,map

时间:2020-07-03 17:44:27      阅读:378      评论:0      收藏:0      [点我收藏+]

1.对多个属性去重

List newList = list.stream().collect(
        Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
                        o ->    o.getProductName() + ";" +
                                o.getManufactureName() +";"+
                                o.getShopSign() +";"+
                                o.getSpecComment() +";"+
                                o.getProductTypeCode() +";"+
                                o.getWeight() +";"+
                                o.getWarehouseCode() +";"+
                                o.getPackCode()
                        ))
                ), ArrayList::new));

2.分组

//根据多个属性分组
Map<String, List<String>> groupBy = voList.stream().collect(Collectors.groupingBy(CountDefaultOrderVo::getProviderCode,
                    Collectors.mapping(CountDefaultOrderVo::getPackCode, Collectors.toList())));
//根据某一个属性分组                 
Map<Integer, List<TestStreamModel>> map = list.stream().collect(Collectors.groupingBy(t -> t.getGrade()));  

3.过滤

List list = new ArrayList();
list.add("1");
List collect = list.stream().filter(x -> {
    if (!("0.5".equals(x) || "1".equals(x))) {
        return true;
    }
    return false;
}).collect(Collectors.toList());

4.list转map

Map result1 = list.stream().collect(Collectors.toMap(对象::属性1, 对象::属性2));

5.map转list

map.entrySet().stream().map(e -> new Person(e.getKey(),e.getValue())).collect(Collectors.toList());

6.遍历map

map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));

 

 

java8 Lambda Stream操作list,map

原文:https://www.cnblogs.com/cnndevelop/p/13231415.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!