文章目录
- Java Stream流 List转换Map方法汇总合集(大概是最全吧)
- List -> Map< String,List>
- 示例一
- 示例二
- List -> Map
- List -> Map
- toMap()无重复key
- 示例一
- 示例二
- toMap()有重复key
- 示例一
- 示例二
- toMap()空指针异常
- 解决办法一
- 解决办法二
- 解决办法三
- List -> Map< T, List >
- groupingBy()
- partitioningBy()
Java Stream流 List转换Map方法汇总合集(大概是最全吧)
本文主要介绍 Java 中利用 Stream 流将 List 类型数据转换成 Map 类型的几种方法整合汇总,其中包含了toMap()、groupingBy()、partitioningBy()等方法的使用,建议收藏方便查阅,如果有帮助到您,不要吝啬点赞、收藏!
tips:本文没有推荐使用三方类库提供的 List 转 Map 工具类方法,因为在博主公司引入三方依赖需要在评审时说出引用原因,哈哈哈(太麻烦),当然有很多好用三方工具类可以直接完成 List 转 Map 的操作,如有需要,后续博主会单独写一篇三方类库的 List 转 Map 工具类方法博文提供给大家。
您的点赞、收藏、评论是博主输出优质文章的的动力!!!
以下示例会覆盖工作中绝大部分的使用场景,如有没有覆盖到的场景,后续会基于本文持续更新!!所以大家一定要收藏!点赞!欢迎在评论区与博主沟通交流!!
List -> Map< String,List>
示例一
通过Stream流完成转换:
@Testpublic void testList1() {List<String> list = Arrays.asList("s1:01", "s2:02", "s3:03", "s4:04");Pattern DELIMITER = Pattern.compile(":");Map<String, List<String>> map = list.stream().map(DELIMITER::split).collect(Collectors.groupingBy(a -> a[0],Collectors.mapping(a -> a[1], Collectors.toList())));System.out.println(JSONObject.toJSONString(map));}
运行结果:
示例二
通过forEach循环转换:
@Testpublic void testList1() {List<String> list = Arrays.asList("s1:01", "s2:02", "s3:03", "s4:04");Map<String, Set<String>> map = new HashMap<>();list.forEach(location -> {String[] strArr = location.split(":");map.compute(strArr[0], (country, codes) -> {codes = codes == null " />new HashSet<>() : codes;codes.add(strArr[1]);return codes;});});System.out.println(JSONObject.toJSONString(map));}
运行结果:
List -> Map
List -> Map
代码示例(本示例是存在重复key,并且会以首个出现的 key 去覆盖后面出现的冲突 key):
@Testpublic void testList2() {List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 4, 5, 6, 7, 8);Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(k -> k, v -> v, (k1, k2) -> k1));System.out.println("collect:" + collect);}
toMap()无重复key
示例一
List泛型为 Student
对象,示例需要将name属性作为 key,age 属性作为value;
@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29));Map<String, Integer> collect = list.stream().collect(Collectors.toMap(Student::getName, Student::getAge));System.out.println(JSONObject.toJSONString(collect));}
示例二
List泛型为 Student
对象,示例需要将name属性作为 key, Student
对象作为value;
@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29));Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, student -> student));System.out.println(JSONObject.toJSONString(collect));}
运行结果:
toMap()有重复key
当存在重复 key 时,编译器会报错:java.lang.IllegalStateException: Duplicate key xxxx;
出现这种情况就需要在 toMap()
方法中指定 key 来避免冲突;
示例一
解决办法:使用第一个key 覆盖第二个key;
@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 2),new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29));Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, student -> student, (key1, key2) -> key1));System.out.println(JSONObject.toJSONString(collect));}
运行结果(保留了 “num”:11 的对象):
示例二
解决办法:使用第二个key 覆盖第一个key;
@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 2),new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29));Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, student -> student, (key1, key2) -> key2));System.out.println(JSONObject.toJSONString(collect));}
运行结果(保留了 “num”:2 的对象):
toMap()空指针异常
当 value 为空时,使用 toMap()
方法进行转换,编译器会抛出 java.lang.NullPointerException;
@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29),new Student("小田", null, DateUtil.parse("2007-02-21 22:18:56"), null));Map<String, Integer> collect = list.stream().collect(Collectors.toMap(Student::getName, Student::getAge));System.out.println(JSONObject.toJSONString(collect));}
运行结果:
解决办法一
转换时增加判断,如果是null,则设置默认值;
@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29),new Student("小田", null, DateUtil.parse("2007-02-21 22:18:56"), null));Map<String, Integer> collect = list.stream().collect(Collectors.toMap(Student::getName, s -> s.getAge() == null " />-1 : s.getAge()));System.out.println(JSONObject.toJSONString(collect));}
运行结果:
解决办法二
使用 Optional 对值进行包装(和方式一大同小异,就是换了个写法):
@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29),new Student("小田", null, DateUtil.parse("2007-02-21 22:18:56"), null));Map<String, Integer> collect = list.stream().collect(Collectors.toMap(Student::getName,s -> Optional.ofNullable(s.getAge()).orElse(-1)));System.out.println("Optional:"+JSONObject.toJSONString(collect));}
运行结果:
解决办法三
使用collect(Supplier supplier, BiConsumer@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student(“小张”, 17, DateUtil.parse(“2006-10-03 15:18:56”), 11),new Student(“小李”, 15, DateUtil.parse(“2008-03-19 02:18:56”), 5),new Student(“小王”, 16, DateUtil.parse(“2007-02-21 22:18:56”), 29),new Student(null, 12, DateUtil.parse(“2007-02-21 22:18:56”), null));HashMap<Integer, String> collect = list.stream().collect(HashMap::new, (obj, v) -> obj.put(v.getAge(), v.getName()), HashMap::putAll);System.out.println(“collect:” + collect);}
运行结果:
List -> Map< T, List >
groupingBy()
直接根据一个字段或者属性分组也可以直接用 groupingBy()
方法:
@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 2),new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29));Map<String, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getName));System.out.println(JSONObject.toJSONString(collect));}
运行结果:
partitioningBy()
partitioningBy()
可以理解为特殊的 groupingBy()
,key 值为 Boolean类型:
@Testpublic void testList2() {List<Student> list = Lists.newArrayList(new Student("小张", 17, DateUtil.parse("2006-10-03 15:18:56"), 11),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 5),new Student("小李", 15, DateUtil.parse("2008-03-19 02:18:56"), 2),new Student("小王", 16, DateUtil.parse("2007-02-21 22:18:56"), 29));Map<Boolean, List<Student>> collect = list.stream().collect(Collectors.partitioningBy(s -> s.getNum() >= 5));System.out.println(JSONObject.toJSONString(collect));}
运行结果:
感 谢各 位大 佬的 阅读,随手 点赞,日薪 过万~!!!