Program to convert a Map to a Stream in Java - GeeksforGeeks

11 Dec, 2018
A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
Below are various method to convert Map to Stream in Java:
Converting complete Map<Key, Value> 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.Algorithm:Get the Map<Key, Value>.Convert Map<Key, Value> into Set<Key> using Map.entrySet() method.Convert the obtained Set into Stream using Set.stream()Return/Print the Stream of Map.Program:// Java Program to convert// Map<Key, Value> into Stream  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of Integer    public static <K, V> Stream<Map.Entry<K, V> >    convertMapToStream(Map<K, V> 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<Integer, String> 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<Map.Entry<Integer, String> > stream =                                    convertMapToStream(map);          // Print the TreeMap        System.out.println("Stream: "                       + Arrays.toString(stream.toArray()));    }}Output:Map: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Stream: [1=Geeks, 2=forGeeks, 3=A computer Portal]
Converting only the Keyset of the Map<Key, Value> 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.Algorithm:Get the Map<Key, Value>.Convert Map<Key, Value> into Set<Key> using Map.keySet() method.Convert the obtained Set into Stream using Set.stream()Return/Print the Stream of Map.Program:// Java Program to convert// Map<Key, Value> into Stream  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of Integer    public static <K, V> Stream<K>    convertMapToStream(Map<K, V> map)    {          // Return the obtained Stream        return map              // Convert the Map to Set<Key>            .keySet()              // Convert the Set to Stream            .stream();    }      public static void main(String args[])    {          // Create a Map        Map<Integer, String> 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<Integer> stream = convertMapToStream(map);          // Print the TreeMap        System.out.println("Stream: "                     + Arrays.toString(stream.toArray()));    }}Output:Map: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Stream: [1, 2, 3]
Converting only the Value of the Map<Key, Value> 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.Algorithm:Get the Map<Key, Value>.Convert Map<Key, Value> into Set<Value> using Map.values() method.Convert the obtained Set into Stream using Set.stream()Return/Print the Stream of Map.Program:// Java Program to convert// Map<Key, Value> into Stream  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of Integer    public static <K, V> Stream<V>    convertMapToStream(Map<K, V> map)    {          // Return the obtained Stream        return map              // Convert the Map to Set<Value>            .values()              // Convert the Set to Stream            .stream();    }      public static void main(String args[])    {          // Create a Map        Map<Integer, String> 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<String> stream = convertMapToStream(map);          // Print the TreeMap        System.out.println("Stream: "                      + Arrays.toString(stream.toArray()));    }}Output:Map: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Stream: [Geeks, forGeeks, A computer Portal]

Converting complete Map<Key, Value> 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.Algorithm:Get the Map<Key, Value>.Convert Map<Key, Value> into Set<Key> using Map.entrySet() method.Convert the obtained Set into Stream using Set.stream()Return/Print the Stream of Map.Program:// Java Program to convert// Map<Key, Value> into Stream  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of Integer    public static <K, V> Stream<Map.Entry<K, V> >    convertMapToStream(Map<K, V> 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<Integer, String> 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<Map.Entry<Integer, String> > stream =                                    convertMapToStream(map);          // Print the TreeMap        System.out.println("Stream: "                       + Arrays.toString(stream.toArray()));    }}Output:Map: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Stream: [1=Geeks, 2=forGeeks, 3=A computer Portal]

Algorithm:
Get the Map<Key, Value>.Convert Map<Key, Value> into Set<Key> using Map.entrySet() method.Convert the obtained Set into Stream using Set.stream()Return/Print the Stream of Map.
Get the Map<Key, Value>.
Convert Map<Key, Value> into Set<Key> using Map.entrySet() method.
Convert the obtained Set into Stream using Set.stream()
Return/Print the Stream of Map.
Program:
// Java Program to convert// Map<Key, Value> into Stream  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of Integer    public static <K, V> Stream<Map.Entry<K, V> >    convertMapToStream(Map<K, V> 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<Integer, String> 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<Map.Entry<Integer, String> > stream =                                    convertMapToStream(map);          // Print the TreeMap        System.out.println("Stream: "                       + Arrays.toString(stream.toArray()));    }}
Map: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Stream: [1=Geeks, 2=forGeeks, 3=A computer Portal]

Converting only the Keyset of the Map<Key, Value> 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.Algorithm:Get the Map<Key, Value>.Convert Map<Key, Value> into Set<Key> using Map.keySet() method.Convert the obtained Set into Stream using Set.stream()Return/Print the Stream of Map.Program:// Java Program to convert// Map<Key, Value> into Stream  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of Integer    public static <K, V> Stream<K>    convertMapToStream(Map<K, V> map)    {          // Return the obtained Stream        return map              // Convert the Map to Set<Key>            .keySet()              // Convert the Set to Stream            .stream();    }      public static void main(String args[])    {          // Create a Map        Map<Integer, String> 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<Integer> stream = convertMapToStream(map);          // Print the TreeMap        System.out.println("Stream: "                     + Arrays.toString(stream.toArray()));    }}Output:Map: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Stream: [1, 2, 3]

Algorithm:
Get the Map<Key, Value>.Convert Map<Key, Value> into Set<Key> using Map.keySet() method.Convert the obtained Set into Stream using Set.stream()Return/Print the Stream of Map.
Get the Map<Key, Value>.
Convert Map<Key, Value> into Set<Key> using Map.keySet() method.
Convert the obtained Set into Stream using Set.stream()
Return/Print the Stream of Map.
Program:
// Java Program to convert// Map<Key, Value> into Stream  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of Integer    public static <K, V> Stream<K>    convertMapToStream(Map<K, V> map)    {          // Return the obtained Stream        return map              // Convert the Map to Set<Key>            .keySet()              // Convert the Set to Stream            .stream();    }      public static void main(String args[])    {          // Create a Map        Map<Integer, String> 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<Integer> stream = convertMapToStream(map);          // Print the TreeMap        System.out.println("Stream: "                     + Arrays.toString(stream.toArray()));    }}
Map: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Stream: [1, 2, 3]

Converting only the Value of the Map<Key, Value> 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.Algorithm:Get the Map<Key, Value>.Convert Map<Key, Value> into Set<Value> using Map.values() method.Convert the obtained Set into Stream using Set.stream()Return/Print the Stream of Map.Program:// Java Program to convert// Map<Key, Value> into Stream  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of Integer    public static <K, V> Stream<V>    convertMapToStream(Map<K, V> map)    {          // Return the obtained Stream        return map              // Convert the Map to Set<Value>            .values()              // Convert the Set to Stream            .stream();    }      public static void main(String args[])    {          // Create a Map        Map<Integer, String> 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<String> stream = convertMapToStream(map);          // Print the TreeMap        System.out.println("Stream: "                      + Arrays.toString(stream.toArray()));    }}Output:Map: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Stream: [Geeks, forGeeks, A computer Portal]

Algorithm:
Get the Map<Key, Value>.Convert Map<Key, Value> into Set<Value> using Map.values() method.Convert the obtained Set into Stream using Set.stream()Return/Print the Stream of Map.
Get the Map<Key, Value>.
Convert Map<Key, Value> into Set<Value> using Map.values() method.
Convert the obtained Set into Stream using Set.stream()
Return/Print the Stream of Map.
Program:
// Java Program to convert// Map<Key, Value> into Stream  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of Integer    public static <K, V> Stream<V>    convertMapToStream(Map<K, V> map)    {          // Return the obtained Stream        return map              // Convert the Map to Set<Value>            .values()              // Convert the Set to Stream            .stream();    }      public static void main(String args[])    {          // Create a Map        Map<Integer, String> 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<String> stream = convertMapToStream(map);          // Print the TreeMap        System.out.println("Stream: "                      + Arrays.toString(stream.toArray()));    }}
Map: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Stream: [Geeks, forGeeks, A computer Portal]

Java - util package
java-map
java-stream
Java-Stream-programs
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initializing a List in Java
Convert a String to Character Array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class