In this article, a static map is created and initialised in Java using Stream.
Static Map in Java
A static map is a map which is defined as static. It means that the map becomes a class member and can be easily used using class.
Stream In Java
Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
Approach:
- Create a stream and insert 2D values as Key and Value pair in it.
- These values will be later used to instantiate the Map.
- Using the collect() method of Stream, map the values from the stream to the map.
- This can be done with the help of Collectors.toMap() method and the keyMapper and ValueMapper respectively.
Below is the implementation of the above approach:
// Java program to create a static map using Stream import java.util.*; import java.util.stream.Collectors; class GFG { // Declaring and instantiating the static map private static Map<String, String> map = Arrays.stream(new String[][] { { "1", "GFG" }, { "2", "Geek" }, { "3", "GeeksForGeeks" } }) .collect(Collectors.toMap( keyMapper -> keyMapper[0], valueMapper -> valueMapper[1])); // Driver code public static void main(String[] args) { System.out.println(map); } } |
{1=GFG, 2=Geek, 3=GeeksForGeeks}
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Initialize a static Map using Java 9 Map.of()
- Initialize a static Map in Java using Double Brace Initialization
- Initialize a static map in Java with Examples
- Initialize a list in a single line with a specified value using Java Stream
- Static and non static blank final variables in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Understanding storage of static methods and static variables in Java
- Why non-static variable cannot be referenced from a static method in Java
- Understanding "static" in "public static void main" in Java
- Flatten a Stream of Map in Java using forEach loop
- Character Stream Vs Byte Stream in Java
- Difference between Stream.of() and Arrays.stream() method in Java
- Stream map() in Java with examples
- Program to convert a Map to a Stream in Java
- How to convert a Stream into a Map in Java
- Initialize an ArrayList in Java
- Initialize HashMap in Java
- KeyPairGenerator initialize() method in Java with Examples
- How to Fill (initialize at once) an Array in Java?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

