Program to Convert a Map to a Stream in Java
Mapping a map to a stream is an ordinary task in Java, particularly when we need to process the map's contents utilizing the highly effective Stream API that was introduced in Java 8. A stream is a collection of items that can allow different pipelined procedures to achieve a given outcome.
The different ways to convert a map to a stream in Java are listed below.
Approach: Converting complete Map into Stream
The Map.entrySet() function can assist with this, as it provides a Set view of the mappings present in the map. With Java 8, the Set.stream() method can be used for rapidly converting this returned set into a Stream of key-value pairs.
Algorithm:
Step 1: Obtain the Map
Step 2: Apply the Map.entrySet() function to convert Map
Step 3: Call Set.stream() to transform the obtained Set into Stream.
Step 4: Print or Return the Map Stream.
Implementation:
FileName: CompleteMap.java
import java.io.*; import java.util.*; import java.util.stream.*; public class CompleteMap { // Generic function for converting a // list of strings to an integer list public staticStream > convertMapToStream(Map m) { // Return back the acquired stream. return m // Converting from the Map to Set .entrySet() // Converting from the Set to Stream .stream(); } public static void main(String args[]) { // Creating a Map Map m = new HashMap<>(); // Adding data in the Map m.put(1, "Hello"); m.put(2, "Welcome To"); m.put(3, "The World"); // displaying the Map System.out.println("The Map is: " + m); // Converting from the Map to Stream Stream > s = convertMapToStream(m); // displaying the TreeMap System.out.println("The Stream is: " + Arrays.toString(s.toArray())); } }
Output:
The Map is: {1=Hello, 2=Welcome To, 3=The World} The Stream is: [1=Hello, 2=Welcome To, 3=The World]
Approach: Converting only the Keyset of the Map into Stream
This may be performed with the use of the Map.keySet() function, which returns a Set view of the keys in this map. With Java 8, the Set.stream() method can be used to quickly transform this returned set into a Stream of key-value pairs.
Algorithm:
Step 1: Obtain the Map
Step 2: Use the Map.keySet() function to convert Map
Step 3: Use Set.stream() to transform the acquired Set into Stream.
Step 4: Print or return the map's stream.
Implementation:
FileName: KeySet.java
import java.io.*; import java.util.*; import java.util.stream.*; public class KeySet { // Generic function for converting a // list of strings to an integer list public staticStream convertMapToStream(Map m) { // Return back the acquired stream. return m // Converting from the Map to a Set .keySet() // Converting from the Set to a Stream .stream(); } public static void main(String args[]) { // Creating a Map Map m = new HashMap<>(); // Adding items to the Map m.put(1, "Hello"); m.put(2, "Welcome To"); m.put(3, "The World"); // displaying the Map System.out.println("The Map is: " + m); // Converting from the Map to a Stream Stream s = convertMapToStream(m); // displaying the TreeMap System.out.println("The Stream is: " + Arrays.toString(s.toArray())); } }
Output:
The Map is: {1=Hello, 2=Welcome To, 3=The World} The Stream is: [1, 2, 3]
Approach: Converting only the Value of the Map into Stream
The Map.values() function may help with this, as it provides a Set representation of the values present in the map. With Java 8, the Set.stream() method can be used to quickly transform this returned set into a Stream of key-value pairs.
Algorithm:
Step 1: Obtain the Map
Step 2: Use the Map.values() method to convert Map
Step 3: Use Set.stream() to transform the obtained Set into Stream.
Step 4: Print or Return the Map Stream.
Implementation:
FileName: OnlyValue.java
import java.util.*; import java.util.stream.*; public class OnlyValue { // Generic function for converting a // list of strings to an integer list public staticStream convertMapToStream(Map m) { // Return the obtained Stream return m // Convert from the Map to a Set .values() // Convert from the Set to a Stream .stream(); } public static void main(String args[]) { // Creating a Map Map m = new HashMap<>(); // Add entries to the Map m.put(1, "Hello"); m.put(2, "Welcome To"); m.put(3, "The World"); // displaying the Map System.out.println("The Map is: " + m); // Convert from the Map to a Stream Stream s = convertMapToStream(m); // displaying the TreeMap System.out.println("The Stream is: " + Arrays.toString(s.toArray())); } }
Output:
The Map is: {1=Hello, 2=Welcome To, 3=The World} The Stream is: [Hello, Welcome To, The World]