如何使用Stream流将List转换为Map

简介: 如何使用Stream流将List转换为Map

如何使用Stream流将List转换为Map

以下程序用到的基础代码:

final static List<Student> studentList = new ArrayList<Student>();

/**
 * 初始化集合数据
 */
static {
   
   Student stu1 = new Student("0001", "张三", 12, "江苏南京");
   Student stu2 = new Student("0002", "李四", 14, "江苏无锡");
   Student stu3 = new Student("0003", "王二", 11, "浙江台州");
   Student stu4 = new Student("0004", "李五", 12, "浙江温州");


   studentList.add(stu1);
   studentList.add(stu2);
   studentList.add(stu3);
   studentList.add(stu4);
}
AI 代码解读

List<Object> 转化为Map

Map<String, Student> map = studentList.stream().collect(Collectors.toMap(Student::getId, each -> each, (value1, value2) -> value1));
AI 代码解读

List<Object>转化为Map

Map<String, String> map = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getAddress, (value1, value2) -> value1));
AI 代码解读

List<Object>转化为Map>`

Map<Integer, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getAge));
AI 代码解读

List<Object>转化为Map>`

Map<String, List<String>> map3 = studentList.stream().collect(Collectors.toMap(Student::getId, each -> Collections.singletonList(each.getName()), (value1, value2) -> {
   
            List<String> union = new ArrayList<>(value1);
            union.addAll(value2);
            return union;
}));
AI 代码解读

List> 转化为Map

final static List<Map<String, Object>> mapStudentList = new ArrayList<>();
public static void main(String[] args) {
   
        Map<String, Object> map4 = mapStudentList.stream().collect(Collectors.toMap(each -> Objects.toString(each.get("id"), ""), each -> each.get("student"), (key1, key2) -> key1));
    }


    /**
     * 初始化集合数据
     */
    static {
   
        Student stu1 = new Student("0001", "张三", 12, "江苏南京");
        Student stu2 = new Student("0002", "李四", 14, "江苏无锡");
        Student stu3 = new Student("0003", "王二", 11, "浙江台州");
        Student stu4 = new Student("0004", "李五", 12, "浙江温州");


        Map<String, Object> map1 = new HashMap<>();
        map1.put("id", "0001");
        map1.put("student", stu1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("id", "0002");
        map2.put("student", stu2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("id", "0003");
        map3.put("student", stu3);

        Map<String, Object> map4 = new HashMap<>();
        map4.put("id", "0004");
        map4.put("student", stu4);

        mapStudentList.add(map1);
        mapStudentList.add(map2);
        mapStudentList.add(map3);
        mapStudentList.add(map4);
    }
AI 代码解读

List> 转化为Map>

    final static List<Map<String, String>> listMapList = new ArrayList<>();


    public static void main(String[] args) {
   
        Map<String, Map<String, String>> map5 = listMapList.stream().collect(Collectors.toMap(each -> each.get("id"), each -> each, (key1, key2) -> key1));
        System.out.println("map5 = " + map5);

    }

    /**
     * 初始化集合数据
     */
    static {
   
        Map<String, String> map1 = new HashMap<>();
        map1.put("id", "0001");
        map1.put("name", "张三");
        map1.put("age", "12");
        map1.put("address", "江苏南京");

        Map<String, String> map2 = new HashMap<>();
        map2.put("id", "0002");
        map2.put("name", "李四");
        map2.put("age", "14");
        map2.put("address", "江苏无锡");


        Map<String, String> map3 = new HashMap<>();
        map3.put("id", "0003");
        map3.put("name", "王二");
        map3.put("age", "11");
        map3.put("address", "浙江台州");

        Map<String, String> map4 = new HashMap<>();
        map4.put("id", "0004");
        map4.put("name", "李五");
        map4.put("age", "12");
        map4.put("address", "浙江温州");


        listMapList.add(map1);
        listMapList.add(map2);
        listMapList.add(map3);
        listMapList.add(map4);
    }
AI 代码解读

List> 转化为Map

     final static List<Map<String, String>> listmapstringlist = new ArrayList<>();

     public static void main(String[] args) {
   
     Map<String, String> map6 = listmapstringlist.stream().collect(Collectors.toMap(each -> each.get("id"), each -> each.get("name"), (key1, key2) -> key1));

    }

    /**
     * 初始化集合数据
     */
    static {
   
        Map<String, String> map1 = new HashMap<>();
        map1.put("id", "0001");
        map1.put("name", "张三");
        map1.put("age", "12");
        map1.put("address", "江苏南京");

        Map<String, String> map2 = new HashMap<>();
        map2.put("id", "0002");
        map2.put("name", "李四");
        map2.put("age", "14");
        map2.put("address", "江苏无锡");


        Map<String, String> map3 = new HashMap<>();
        map3.put("id", "0003");
        map3.put("name", "王二");
        map3.put("age", "11");
        map3.put("address", "浙江台州");

        Map<String, String> map4 = new HashMap<>();
        map4.put("id", "0004");
        map4.put("name", "李五");
        map4.put("age", "12");
        map4.put("address", "浙江温州");
        listmapstringlist.add(map1);
        listmapstringlist.add(map2);
        listmapstringlist.add(map3);
        listmapstringlist.add(map4);
    }
AI 代码解读
目录
打赏
0
0
0
0
16
分享
相关文章
|
3月前
|
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
83 18
你对Collection中Set、List、Map理解?
只会“有序无序”?面试官嫌弃的List、Set、Map回答!
小米,一位热衷于技术分享的程序员,通过与朋友小林的对话,详细解析了Java面试中常见的List、Set、Map三者之间的区别,不仅涵盖了它们的基本特性,还深入探讨了各自的实现原理及应用场景,帮助面试者更好地准备相关问题。
78 20
|
9月前
|
Dart之集合详解(List、Set、Map)
Dart之集合详解(List、Set、Map)
Redis系列学习文章分享---第七篇(Redis快速入门之消息队列--List实现消息队列 Pubsub实现消息队列 stream的单消费模式 stream的消费者组模式 基于stream消息队列)
Redis系列学习文章分享---第七篇(Redis快速入门之消息队列--List实现消息队列 Pubsub实现消息队列 stream的单消费模式 stream的消费者组模式 基于stream消息队列)
102 0
Java Stream中peek和map不为人知的秘密
本文通过一个Java Stream中的示例,探讨了`peek`方法在流式处理中的应用及其潜在问题。首先介绍了`peek`的基本定义与使用,并通过代码展示了其如何在流中对每个元素进行操作而不返回结果。接着讨论了`peek`作为中间操作的懒执行特性,强调了如果没有终端操作则不会执行的问题。文章指出,在某些情况下使用`peek`可能比`map`更简洁,但也需注意其懒执行带来的影响。
397 2
Java Stream中peek和map不为人知的秘密
|
6月前
|
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
53 5
java集合框架复习----(4)Map、List、set
这篇文章是Java集合框架的复习总结,重点介绍了Map集合的特点和HashMap的使用,以及Collections工具类的使用示例,同时回顾了List、Set和Map集合的概念和特点,以及Collection工具类的作用。
java集合框架复习----(4)Map、List、set
|
7月前
|
Stream很好,Map很酷,但答应我别滥用toMap()!
【8月更文挑战第27天】在Java的世界里,Stream API和Map数据结构无疑是现代编程中的两大瑰宝。Stream API以其函数式编程的优雅和强大的数据处理能力,让集合操作变得简洁而高效;而Map则以其键值对的存储方式,为数据的快速检索和更新提供了便利。然而,当这两者相遇,特别是当我们试图通过Stream的toMap()方法将流中的元素转换为Map时,一些潜在的问题和陷阱便悄然浮现。今天,我们就来深入探讨一下这个话题,并探讨如何更加安全、高效地利用这些强大的工具。
98 0
老程序员分享:List、Map、Set之间的联系与区别:
老程序员分享:List、Map、Set之间的联系与区别:
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等