如何使用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);
}

List<Object> 转化为Map

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

List<Object>转化为Map

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

List<Object>转化为Map>`

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

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

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

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

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);
    }
相关文章
|
4天前
|
存储 安全 Java
java集合框架及其特点(List、Set、Queue、Map)
java集合框架及其特点(List、Set、Queue、Map)
|
4天前
|
存储 安全 Java
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
|
4天前
|
JSON 数据格式
使用 Gson 将 Map、List等转换为json string
使用 Gson 将 Map、List等转换为json string
13 0
|
4天前
|
Java API
List转Map(id为key,list为value)
List转Map(id为key,list为value)
13 0
|
4天前
|
存储 安全 Java
Java集合详解(List、Map、Set)
Java集合详解(List、Map、Set)
34 4
|
4天前
|
存储 C语言 C++
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
|
4天前
|
Java
Java8中List转Map的几种方式
Java8中List转Map的几种方式
17 1
|
4天前
|
Java API
java 对象list 使用stream进行过滤
在Java中,你可以使用Stream API对对象列表进行过滤。假设你有一个`List<MyObject>`,并且你想根据某些条件过滤出特定的对象。以下是一个示例: ```java import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<MyObject> myObjects = ... // 初始化你的对象列表 List<MyObject> filter
|
4天前
|
存储 安全 算法
Java一分钟之-Java集合框架入门:List接口与ArrayList
【5月更文挑战第10天】本文介绍了Java集合框架中的`List`接口和`ArrayList`实现类。`List`是有序集合,支持元素重复并能按索引访问。核心方法包括添加、删除、获取和设置元素。`ArrayList`基于动态数组,提供高效随机访问和自动扩容,但非线程安全。文章讨论了三个常见问题:索引越界、遍历时修改集合和并发修改,并给出避免策略。通过示例代码展示了基本操作和安全遍历删除。理解并正确使用`List`和`ArrayList`能提升程序效率和稳定性。
11 0
|
4天前
|
存储 安全 Java
【JAVA基础篇教学】第八篇:Java中List详解说明
【JAVA基础篇教学】第八篇:Java中List详解说明