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 Initialization:
- The first brace creates a new Anonymous Inner Class. These inner classes are capable of accessing the behaviour of their parent class. So, in our case, we are actually creating a subclass of HashMap class, so this inner class is capable of using put() method.
- The second braces are instance initializers. The code an instance initializers inside is executed whenever an instance is created.
Approach:
- Pass the map values as Key and Value pair in the Double braces.
- A static factory Map instance is returned.
- Store it in Map and use.
Below is the implementation of the above approach:
Example 1:
// Java program to create a static map // using Double Brace Initialization import java.util.*; class GFG { // Declaring and instantiating the static map // using Double Brace Initialization private static Map<String, String> map = new HashMap<String, String>() {{ put("1", "GFG"); put("2", "Geek"); put("3", "GeeksForGeeks"); }}; // Driver code public static void main(String[] args) { System.out.println(map); } } |
{1=GFG, 2=Geek, 3=GeeksForGeeks}
Example 2: To show with 10 key-value pairs
// Java program to create a static map // using Double Brace Initialization import java.util.*; class GFG { // Declaring and instantiating the static map // using Double Brace Initialization private static Map<String, String> map = new HashMap<String, String>() {{ put("1", "GFG"); put("2", "Geek"); put("3", "GeeksForGeeks"); put("4", "G"); put("5", "e"); put("6", "e"); put("7", "k"); put("8", "s"); put("9", "f"); put("10", "o"); }}; // Driver code public static void main(String[] args) { System.out.println(map); } } |
{1=GFG, 2=Geek, 3=GeeksForGeeks, 4=G, 5=e, 6=e, 7=k, 8=s, 9=f, 10=o}
Related Articles:
- Initialize a static map in Java with Examples
- Initialize a static Map using Stream in Java
- Initialize a static Map using Java 9 Map.of()
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:
- Double Brace Initialization in Java
- Initialize a static Map using Java 9 Map.of()
- Initialize a static Map using Stream in Java
- Initialize a static map in Java with Examples
- 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
- Internal static variable vs. External static variable with Examples in C
- Java Guava | Doubles.indexOf(double[] array, double target) method with Examples
- Java Guava | Doubles.indexOf(double[] array, double[] target) method with Examples
- Initialize a list in a single line with a specified value using Java Stream
- Instance Initialization Block (IIB) in Java
- Order of execution of Initialization blocks and Constructors in Java
- Initialization of local variable in a conditional block in Java
- Initialize an ArrayList in Java
- Initialize HashMap in Java
- KeyPairGenerator initialize() method in Java with Examples
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.

