java8 Lambda Stream操作list,map

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));

 

 

posted @   大浪不惊涛  阅读(2760)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示