java8 stream流对List的常用操作 List转Map、List去重、List对象去重

写在前面:各位看到此博客的小伙伴,如有不对的地方请及时通过私信我或者评论此博客的方式指出,以免误人子弟。多谢!

  记录一下使用stream流对List的常用操作以便以后忘了的时候查看,先准备下测试数据

public static List<Goods> getList(){
        Goods goods = Goods.builder().id(1L).goodsName("测试").goodsAddress("济南").build();
        Goods goods1 = Goods.builder().id(2L).goodsName("测试1").goodsAddress("济南").build();
        Goods goods2 = Goods.builder().id(3L).goodsName("测试2").goodsAddress("日照").build();
        return Arrays.asList(goods,goods1,goods2);
    }

先记录下List转Map

public static void listToMap(){
        List<Goods> goods = getList();
        // 指定key-value,value是对象中的某个属性值
        Map<Long, String> map = goods.stream().collect(Collectors.toMap(Goods::getId,
                Goods::getGoodsName));
        System.out.println(map);
        // 指定key-value,value是对象本身,good->good 是一个返回本身的lambda表达式
        Map<Long, Goods> map1 = goods.stream().collect(Collectors.toMap(Goods::getId,
                good -> good));
        System.out.println(map1);
        // 指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身
        Map<Long, Goods> map2 = goods.stream().collect(Collectors.toMap(Goods::getId,
                Function.identity()));
        System.out.println(map2);
        // 指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身,key 冲突的解决办法,这里选择第一个key覆盖第二个key。
        Map<String, Goods> map3 = goods.stream().collect(Collectors.toMap(Goods::getGoodsAddress,
                Function.identity(),
                (key1, key2) -> key1));
        System.out.println(map3);
    }

执行结果:

  1. {1=测试, 2=测试1, 3=测试2}
  2. {1=Goods(id=1, goodsName=测试, goodsAddress=济南, createdTime=null), 2=Goods(id=2, goodsName=测试1, goodsAddress=济南, createdTime=null), 3=Goods(id=3, goodsName=测试2, goodsAddress=日照, createdTime=null)}
  3. {1=Goods(id=1, goodsName=测试, goodsAddress=济南, createdTime=null), 2=Goods(id=2, goodsName=测试1, goodsAddress=济南, createdTime=null), 3=Goods(id=3, goodsName=测试2, goodsAddress=日照, createdTime=null)}
  4. {济南=Goods(id=1, goodsName=测试, goodsAddress=济南, createdTime=null), 日照=Goods(id=3, goodsName=测试2, goodsAddress=日照, createdTime=null)}

再记录一下List去重

public static void listDistinct(){
        // list集合去重
        List<String> list = Arrays.asList("a","b","c","a");
        List<String> collect = list.stream().distinct().collect(Collectors.toList());
        System.out.println(collect);
        // list对象集合根据属性去重得到属性列表
        List<Goods> goods = getList();
        List<String> addressList =
        goods.stream().map(Goods::getGoodsAddress).distinct().collect(Collectors.toList());
        // list对象集合根据对象属性去重
        List<Goods> goods = getList();
        List<Goods> collect1 = goods.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Goods::getGoodsAddress))), ArrayList::new
                )
        );
        System.out.println(collect1);
    }

执行结果:

  1. [a, b, c]
  2. [Goods(id=3, goodsName=测试2, goodsAddress=日照, createdTime=null), Goods(id=1, goodsName=测试, goodsAddress=济南, createdTime=null)]

最后记录下List的其它处理

public static void list2List(){
        // list元素处理后转集合
        List<String> list = Arrays.asList("a","b","c","a");
        List<String> collect = list.stream().map(str -> str + 1).collect(Collectors.toList());
        System.out.println(collect);
        List<Goods> goods = getList();
        // list对象转list对象属性集合
        List<String> collect1 =
                goods.stream().map(Goods::getGoodsAddress).collect(Collectors.toList());
        System.out.println(collect1);
        // list对象转list对象属性集合并去重
        List<String> collect2 =
                goods.stream().map(Goods::getGoodsAddress).distinct().collect(Collectors.toList());
        System.out.println(collect2);
        // 集合中过滤掉id不为2的属性
        List<Goods> collect3 =
                goods.stream().filter(g -> !g.getId().equals(2L)).collect(Collectors.toList());
        System.out.println(collect3);

    }

执行结果:

  1. [a1, b1, c1, a1]
  2. [济南, 济南, 日照]
  3. [济南, 日照]
  4. [Goods(id=1, goodsName=测试, goodsAddress=济南, createdTime=null), Goods(id=3, goodsName=测试2, goodsAddress=日照, createdTime=null)]

 

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值