当前位置: 代码迷 >> 综合 >> java8 stream流对List的常用操作 List转Map、List去重、List对象去重
  详细解决方案

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

热度:24   发布时间:2024-03-08 10:58:28.0

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

  记录一下使用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<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)]

 

 

 

  相关解决方案