当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java List转Map用法及代码示例


List 是 Collection 的子接口。它是一个有序的对象集合,其中可以存储重复的值。由于 List 保留插入顺序,因此它允许位置访问和插入元素。列表接口由 ArrayListLinkedListVectorStack 类实现。

Map接口表示键和值之间的映射。 Map 接口不是 Map 接口的子类型集合接口。因此它的行为与其他集合类型有点不同。
mapinterface

例子:

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. 创建一个空Map
    3. 迭代列表中的项目并将每个项目添加到 Map 中。
    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<Student> 
                lt = new ArrayList<Student>(); 
      
            // 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<Integer, String> 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() 将其转换为 Map。
    Approach:
    1. 获取要转换为Map的List
    2. 使用List.stream()方法将List转换为流
    3. 借助 Collectors.toMap() 方法创建Map
    4. 使用stream.collect()方法收集形成的Map
    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<Student> 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<Integer, String> 
                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. 使用Collector创建MultiMap。groupingBy():

    方法:

    1. 获取要转换为Map的List
    2. 使用List.stream()方法将List转换为流
    3. 借助 Collectors.groupingBy() 方法创建Map
    4. 使用stream.collect()方法收集形成的Map
    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<Student> lt = new ArrayList<Student>(); 
      
            // 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<Integer, List<String> > 
                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]}
    


相关用法


注:本文由纯净天空筛选整理自Rajput-Ji大神的英文原创作品 Program to Convert List to Map in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。