用 Java 将地图转换成流的程序

原文:https://www . geesforgeks . org/program-to-convert-a-map-a-stream-in-Java/

一个是一个支持各种方法的对象序列,这些方法可以被流水线化以产生期望的结果。

以下是用 Java 将地图转换为流各种方法:

  1. Converting complete Map into Stream: This can be done with the help of Map.entrySet() method which returns a Set view of the mappings contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set.stream() method.

    算法:

    1. 获取地图。
    2. 使用 Map.entrySet()方法将地图转换为集合。
    3. 使用 Set.stream()将获得的集合转换为流
    4. 返回/打印地图流。

    程序:

    ```java // Java Program to convert // Map into Stream

    import java.util.; import java.util.stream.;

    class GFG {

    // Generic function to convert List of     // String to List of Integer     public static Stream >     convertMapToStream(Map map)     {

    // Return the obtained Stream         return map

    // Convert the Map to Set             .entrySet()

    // Convert the Set to Stream             .stream();     }

    public static void main(String args[])     {

    // Create a Map         Map map = new HashMap<>();

    // Add entries to the Map         map.put(1, "Geeks");         map.put(2, "forGeeks");         map.put(3, "A computer Portal");

    // Print the Map         System.out.println("Map: " + map);

    // Convert the Map to Stream         Stream > stream =                                     convertMapToStream(map);

    // Print the TreeMap         System.out.println("Stream: "                        + Arrays.toString(stream.toArray()));     } } ```

    Output:

    ```java Map: {1=Geeks, 2=forGeeks, 3=A computer Portal} Stream: [1=Geeks, 2=forGeeks, 3=A computer Portal]

    ```

  2. Converting only the Keyset of the Map into Stream: This can be done with the help of Map.keySet() method which returns a Set view of the keys contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set.stream() method.

    算法:

    1. 获取地图。
    2. 使用 Map.keySet()方法将地图转换为集合。
    3. 使用 Set.stream()将获得的集合转换为流
    4. 返回/打印地图流。

    程序:

    ```java // Java Program to convert // Map into Stream

    import java.util.; import java.util.stream.;

    class GFG {

    // Generic function to convert List of     // String to List of Integer     public static Stream     convertMapToStream(Map map)     {

    // Return the obtained Stream         return map

    // Convert the Map to Set             .keySet()

    // Convert the Set to Stream             .stream();     }

    public static void main(String args[])     {

    // Create a Map         Map map = new HashMap<>();

    // Add entries to the Map         map.put(1, "Geeks");         map.put(2, "forGeeks");         map.put(3, "A computer Portal");

    // Print the Map         System.out.println("Map: " + map);

    // Convert the Map to Stream         Stream stream = convertMapToStream(map);

    // Print the TreeMap         System.out.println("Stream: "                      + Arrays.toString(stream.toArray()));     } } ```

    Output:

    ```java Map: {1=Geeks, 2=forGeeks, 3=A computer Portal} Stream: [1, 2, 3]

    ```

  3. Converting only the Value of the Map into Stream: This can be done with the help of Map.values() method which returns a Set view of the values contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set.stream() method.

    算法:

    1. 获取地图。
    2. 使用 Map.values()方法将地图转换为集合。
    3. 使用 Set.stream()将获得的集合转换为流
    4. 返回/打印地图流。

    程序:

    ```java // Java Program to convert // Map into Stream

    import java.util.; import java.util.stream.;

    class GFG {

    // Generic function to convert List of     // String to List of Integer     public static Stream     convertMapToStream(Map map)     {

    // Return the obtained Stream         return map

    // Convert the Map to Set             .values()

    // Convert the Set to Stream             .stream();     }

    public static void main(String args[])     {

    // Create a Map         Map map = new HashMap<>();

    // Add entries to the Map         map.put(1, "Geeks");         map.put(2, "forGeeks");         map.put(3, "A computer Portal");

    // Print the Map         System.out.println("Map: " + map);

    // Convert the Map to Stream         Stream stream = convertMapToStream(map);

    // Print the TreeMap         System.out.println("Stream: "                       + Arrays.toString(stream.toArray()));     } } ```

    Output:

    ```java Map: {1=Geeks, 2=forGeeks, 3=A computer Portal} Stream: [Geeks, forGeeks, A computer Portal]

    ```