Initialize a static Map using Java 9 Map.of()

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 he 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

{3=GeeksForGeeks, 2=Geek, 1=GFG}

Example 2: To show the error when 10 key-value pairs are given

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.