Initialize a static Map using Java 9 Map.of()
Last Updated :
12 Jul, 2025
In this article, a
static map is created and initialised in Java using Java 9.
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.
Java 9 feature - Map.of() method
In
Java 9, Map.of() was introduced which is a convenient way to create instances of Map interface. It can hold up to 10 key-value pairs.
Approach:
- Pass the map values as Key and Value pair in the Map.of() method.
- A static factory Map instance is returned.
- Store it in Map and use.
Below is the implementation of the above approach:
Example 1:
Java
// Java program to create a static map using Java 9
import java.util.*;
class GFG {
// Declaring and instantiating the static map
private static Map<String, String> map
= Map.of("1", "GFG",
"2", "Geek",
"3", "GeeksForGeeks");
// Driver code
public static void main(String[] args)
{
System.out.println(map);
}
}
Output:
{3=GeeksForGeeks, 2=Geek, 1=GFG}
Example 2: To show the error when 10 key-value pairs are given
Java
// Java program to create a static map using Java 9
import java.util.*;
class GFG {
// Declaring and instantiating the static map
private static Map<String, String> map
= Map.of("1", "GFG",
"2", "Geek",
"3", "GeeksForGeeks",
"4", "G",
"5", "e",
"6", "e",
"7", "k",
"8", "s",
"9", "f",
"10", "o");
// Driver code
public static void main(String[] args)
{
System.out.println(map);
}
}
Output:
{10=o, 9=f, 8=s, 7=k, 6=e, 5=e, 4=G, 3=GeeksForGeeks, 2=Geek, 1=GFG}
Example 3: To show the error when more than 10 key-value pairs are given
Java
// Java program to create a static map using Java 9
import java.util.*;
class GFG {
// Declaring and instantiating the static map
private static Map<String, String> map
= Map.of("1", "GFG",
"2", "Geek",
"3", "GeeksForGeeks",
"4", "G",
"5", "e",
"6", "e",
"7", "k",
"8", "s",
"9", "f",
"10", "o",
"11", "r");
// Driver code
public static void main(String[] args)
{
System.out.println(map);
}
}
Compilation Error:
Main.java:12: error: no suitable method found for
of(String, String,
String, String,
String, String,
String, String,
String, String,
String, String,
String, String,
String, String,
String, String,
String, String,
String, String)
1 error
Related Articles:
Similar Reads
Initialize a static Map using Stream in Java In this article, a static map is created and initialized 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
2 min read
Initialize a static map in Java with Examples In this article, a static map is created and initialized 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. Method 1: Creating a static map variable. Instantiating it in a static block. Below is the implementati
1 min read
Initialize a static Map in Java using Double Brace Initialization In this article, a static map is created and initialised in Java using Double Brace Initialization. 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. Double Brace Initialization In Double Brace Initia
2 min read
Flatten a Stream of Map in Java using forEach loop Given a Stream of Map in Java, the task is to Flatten the Stream using forEach() method. Examples: Input: map = {1=[1, 2], 2=[3, 4, 5, 6], 3=[7, 8, 9]} Output: [1, 2, 3, 4, 5, 6, 7, 8, 9] Input: map = {1=[G, e, e, k, s], 2=[F, o, r], 3=[G, e, e, k, s]} Output: [G, e, e, k, s, F, o, r] Approach: Get
3 min read
How to Iterate Any Map in Java? In Java, a Map is a data structure that is used to store key-value pairs. Understanding how to iterate over the elements of a map plays a very important role. There are 5 ways to iterate over the elements of a map, and in this article, we are going to discuss all of them.Note: We cannot iterate over
5 min read
Map Values() Method in Java With Examples Map Values() method returns the Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. Syntax: map.values() Parameter: This method does not take any parameter. Return value: It returns a collect
2 min read