📌  相关文章
📜  Java中将列表转换为地图的程序

📅  最后修改于: 2022-05-13 01:54:56.913000             🧑  作者: Mango

Java中将列表转换为地图的程序

List 是 Collection 的子接口。它是可以存储重复值的对象的有序集合。由于 List 保留了插入顺序,它允许元素的位置访问和插入。 List 接口由 ArrayList、LinkedList、Vector 和 Stack 类实现。

Java.util.Map 接口表示键和值之间的映射。 Map 接口不是 Collection 接口的子类型。因此,它的行为与其他集合类型略有不同。
地图界面

例子:

Input: List : [1="1", 2="2", 3="3"]
Output: Map : {1=1, 2=2, 3=3}

Input: List : [1="Geeks", 2="for", 3="Geeks"]
Output: Map : {1=Geeks, 2=for, 3=Geeks}

以下是在Java中将 List 转换为 Map 的各种方法。为此,假设 List 的每个元素都有一个标识符,该标识符将用作生成的 Map 中的键。

  1. 按列表对象使用:

    方法:

    1. 获取要转换为 Map 的 List
    2. 创建一个空地图
    3. 遍历列表中的项目并将它们中的每一个添加到地图中。
    4. 返回形成的 Map
    // Java program for list convert in map
    // with the help of Object method
      
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.HashMap;
      
    // create a list
    class Student {
      
        // id will act as Key
        private Integer id;
      
        // name will act as value
        private String name;
      
        // create curstuctor for reference
        public Student(Integer id, String name)
        {
      
            // assign the value of id and name
            this.id = id;
            this.name = name;
        }
      
        // return private variable id
        public Integer getId()
        {
            return id;
        }
      
        // return private variable name
        public String getName()
        {
            return name;
        }
    }
      
    // main class and method
    public class GFG {
      
        // main Driver
        public static void main(String[] args)
        {
      
            // create a list
            List
                lt = new ArrayList();
      
            // add the member of list
            lt.add(new Student(1, "Geeks"));
            lt.add(new Student(2, "For"));
            lt.add(new Student(3, "Geeks"));
      
            // create map with the help of
            // Object (stu) method
            // create object of Map class
            Map map = new HashMap<>();
      
            // put every value list to Map
            for (Student stu : lt) {
                map.put(stu.getId(), stu.getName());
            }
      
            // print map
            System.out.println("Map  : " + map);
        }
    }
    
    输出:
    Map  : {1=Geeks, 2=For, 3=Geeks}
    
  2. 使用 Collectors.toMap() 方法:此方法包括创建学生对象列表,并使用 Collectors.toMap() 将其转换为地图。
    方法:
    1. 获取要转换为 Map 的 List
    2. 使用 List.stream() 方法将列表转换为流
    3. 在 Collectors.toMap() 方法的帮助下创建地图
    4. 使用 stream.collect() 方法收集形成的地图
    5. 返回形成的 Map
    // Java program for list convert  in map
    // with the help of Collectors.toMap() method
      
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.stream.Collectors;
      
    // create a list
    class Student {
      
        // id will act as Key
        private Integer id;
      
        // name will act as value
        private String name;
      
        // create curstuctor for reference
        public Student(Integer id, String name)
        {
      
            // assign the value of id and name
            this.id = id;
            this.name = name;
        }
      
        // return private variable id
        public Integer getId()
        {
            return id;
        }
      
        // return private variable name
        public String getName()
        {
            return name;
        }
    }
      
    // main class and method
    public class GFG {
      
        // main Driver
        public static void main(String[] args)
        {
      
            // create a list
            List lt = new ArrayList<>();
      
            // add the member of list
            lt.add(new Student(1, "Geeks"));
            lt.add(new Student(2, "For"));
            lt.add(new Student(3, "Geeks"));
      
            // create map with the help of
            // Collectors.toMap() method
            LinkedHashMap
                map = lt.stream()
                          .collect(
                              Collectors
                                  .toMap(
                                      Student::getId,
                                      Student::getName,
                                      (x, y)
                                          -> x + ", " + y,
                                      LinkedHashMap::new));
      
            // print map
            map.forEach(
                (x, y) -> System.out.println(x + "=" + y));
        }
    }
    
    输出:
    1=Geeks
    2=For
    3=Geeks
    
  3. 使用 Collectors.groupingBy() 创建 MultiMap:

    方法:

    1. 获取要转换为 Map 的 List
    2. 使用 List.stream() 方法将列表转换为流
    3. 借助 Collectors.groupingBy() 方法创建地图
    4. 使用 stream.collect() 方法收集形成的地图
    5. 返回形成的 Map
    // Java program for list convert  in map
    // with the help of Collectors.groupingBy() method
      
    import java.util.*;
    import java.util.stream.Collectors;
      
    // create a list
    class Student {
      
        // id will act as Key
        private Integer id;
      
        // name will act as value
        private String name;
      
        // create curstuctor for reference
        public Student(Integer id, String name)
        {
      
            // assign the value of id and name
            this.id = id;
            this.name = name;
        }
      
        // return private variable id
        public Integer getId()
        {
            return id;
        }
      
        // return private variable name
        public String getName()
        {
            return name;
        }
    }
      
    // main class and method
    public class GFG {
      
        // main Driver
        public static void main(String[] args)
        {
      
            // create a list
            List lt = new ArrayList();
      
            // add the member of list
            lt.add(new Student(1, "Geeks"));
            lt.add(new Student(1, "For"));
            lt.add(new Student(2, "Geeks"));
            lt.add(new Student(2, "GeeksForGeeks"));
      
            // create map with the help of
            // Object (stu) method
            // create object of Multi Map class
      
            // create multimap and store the value of list
            Map >
                multimap = lt
                               .stream()
                               .collect(
                                   Collectors
                                       .groupingBy(
                                           Student::getId,
                                           Collectors
                                               .mapping(
                                                   Student::getName,
                                                   Collectors
                                                       .toList())));
      
            // print the multiMap
            System.out.println("MultiMap = " + multimap);
        }
    }
    
    输出:
    MultiMap = {1=[Geeks, For], 2=[Geeks, GeeksForGeeks]}