将Java Maps转换为List

将Java Maps转换为List

Map是一个将键映射到值或属性值对的集合对象。列表是一个有序对象集合,List可以包含重复值。Map有两个值(键和值),而List只有一个值(一个元素)。因此我们可以生成两个列表,如下所示:

  • 值的列表和
  • 来自Map的键的列表。

假设“ map” 是Map的实例,因此常规情况下我们都知道它包含集合和值对。 因此他们定义如下:

  • map.values() 将返回map的值的集合。
  • map.keySet() 将返回map的键的集合。

方法:

  1. 在ArrayList构造函数参数中传递键的集合
  2. 将由map.values ()方法生成的map值集合传递到ArrayList构造函数参数中
  3. 使用Streams API(仅适用于JDK 8及以后版本)

让我们讨论其中的每一个

方法1: 在ArrayList构造函数参数中传递键的集合

过程: 我们可以通过将map.keySet()方法生成的键集合传递给ArrayList构造函数参数来将Map键转换为Key的List,如下所示

map.values(); //现在这里e将传递我们映射的键集,因此它变成
map.values(map.keySet()); //现在我们只需要将它存储到List中,因此创建对象
List ListofKeys = new ArrayList(map.keySet()); //现在我们完成转换。

语法: 因此,它如下所示:

List ListofKeys = new ArrayList(map.keySet());

方法2: List ListofKeys = new ArrayList(map.keySet());

我们可以通过将由map.values()方法生成的映射值集合传递到ArrayList构造函数参数来将Map键转换为值的List。

语法: 因此,它如下所示:

List Listofvalues = new ArrayList(map.values());

例子

//Java将Map转换为List的程序
 
//导入所需类
import java.util.*;
 
class GFG {
 
    //方法1
    public static void main(String[] args)
    {
 
        //创建HashMap
        HashMap<String, Integer> hs = new HashMap<>();
 
        //向hashMap中添加元素
        hs.put("Geeks", 1);
        hs.put("for", 2);
        hs.put("Geeks", 3);
        hs.put("Computer", 4);
        hs.put("Science", 5);
        hs.put("Portal", 6);
 
        //调用方法
        MapValuesToList obj = new MapValuesToList(hs);
 
        //通过for循环将其存储到List中
        List<Integer> mapvaltolist = obj.mapvaltolist(hs);
 
        //打印
        for (Integer integerList : mapvaltolist) {
 
            //打印ArrayList
            System.out.println(integerList);
        }
    }
 
    //方法2
    //将Map转换为List
    public List<String>
    mapvaltolist(Map<String, Integer> map)
    {
 
        //使用Collection
        Collection<Integer> val = map.values();
 
        //创建一个ArrayList
        ArrayList<Integer> al = new ArrayList<>(values);
 
        return al;
    }
}

输出:

1
2
3
4
5
6

方法3: 使用Streams API

stream() 方法返回一个由 Map.keySet() 返回的映射键集合的键流。Stream 类的 collect() 方法将键流收集到 List 中。Collectors.toCollection(ArrayList::new) 传递给 collect() 方法以收集新的 ArrayList。您也可以使用 Stream API 将映射键和值转换为各自的列表。语法如下所示:

List ListofKeys = map.keyset().stream().collect(Collectors.toCollection(ArrayList::new));

List Listofvalues= map.values().stream().collect(Collectors.toCollection(ArrayList::new));

注意: 您可以将 Stream 元素收集到 ArrayList、LinkedList 或任何其他 List 实现中。

实现:

输入 N 个学生的学号和姓名。首先,我们创建一个 Map,其中 Rollno 是键,因为 Rollno 是唯一的,Name 是 Value for Map,然后将此 Map 转换为值列表和键列表。生成的键列表包含学生的 RollNo,值列表包含学生的 Name。

示例

// Java program to Convert Map to List
 
// Importing required classes
import java.util.*;
// Importing stream sub-package
import java.util.stream.*;
 
// Main class
// MapToList
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Scanner class to take input of key-value pairs
        Scanner sc = new Scanner(System.in);
 
        // Creating a Hashmap which maps rollno with student
        // name
        Map<String, String> map
            = new HashMap<String, String>();
 
        // Command for better usability
        System.out.println("Enter No of Students");
 
        // Taking input to Hashmap
        // via iterating using for loop
        int noOfStudents = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < noOfStudents; i++) {
 
            String input = sc.nextLine();
            String[] studentdata = input.split(" ");
 
            String rollno = studentdata[0];
            String name = studentdata[1];
 
            // Simply inserting received pairs to Map
            map.put(rollno, name);
        }
 
        // Now first create list of keys and values
        List<String> ListofKeys = null;
        List<String> ListofValues = null;
 
        // Now converting hashMap to List of keys and values
        ListofKeys = map.keySet().stream().collect(
            Collectors.toCollection(ArrayList::new));
        ListofValues = map.values().stream().collect(
            Collectors.toCollection(ArrayList::new));
 
        // lastly printing List of rollno and name of
        // students
        System.out.println("List of RollNo of Students");
        System.out.println(ListofKeys.toString());
 
        System.out.println("List of Name of Students");
        System.out.println(ListofValues.toString());
    }
}

输出:

将Java Maps转换为List

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程