Java

How to Set Up a Static Map in Java

In programming, handling the data is a common task that can be made effective via multiple approaches. In such situations, setting or initializing a “static” map comes into effect in Java that assists the programmer to invoke the added code functionalities directly without instantiating the class, thereby reducing the code complexity.

This article will elaborate on the approaches to initialize /set up a static map in Java

What is a Static Map?

A map specified as “static” becomes a static map such that it can be accessed without instantiating (creating a class object) the class.

How to Set Up a Static Map in Java?

A map can be set up in Java via the below-given approaches:

Firstly, import the following package in both the approaches of this article to allow access to all the functionalities within the “java.util” package:

import java.util.*;

Approach 1: Setting up a Static Map Using “Static Initialization Block”

This approach utilizes a “static” initialization block to define a static map specified before.

Example

Overview of the below-provided code snippet:

public class Staticblock {
 static final Map staticMap;
 static {
  staticMap = new HashMap();
  staticMap.put(1, "Java");
  staticMap.put(2, "Programming");
  staticMap.put(3, "Language");
}
public static void main(String[] args){
 System.out.println(staticMap);
}}

In the above code lines:

  • Firstly, specify the map that needs to be initialized as “static” having the keys as “Integer” and the values as “String” i.e., “<Integer, String>”.
  • Now, specify a “static” initialization block to allocate the map as “static”.
  • In this block, append the stated entries in the map via the associated “put()” method.
  • In “main”, invoke the defined “static” map directly.

Output

In the executed output, it can be visualized that the initialized “static” map is invoked directly without instantiating it.

Approach 2: Setting up a Static Map Using the “Static Method”

In this approach, a “static” method can be used instead to set up a “static” map by assigning it to the specified map.

Example

Consider the following block of code:

public class Staticblock {
 private static final Map<Object, String> staticMap = actualMap();
 private static Map<Object, String> actualMap() {
  Map<Object, String> map = new HashMap();
  map.put(1, "Java");
  map.put(2, "Programming");
  map.put("three", "Language");
  return Collections.unmodifiableMap(map);
}
public static void main(String[] args){
 System.out.println(staticMap);
}}

Based on these code lines, follow the below-given steps:

  • Specify a map having the stated “key-value” pairs and allocate it to the “actualMap()” method/function.
  • Note: The “Object” type is compatible with both the “Integer” and “String” data type values.
  • In the next step, replicate the same format of the specified map in this method.
  • In its (method) definition, add the stated values to the map using the “put()” method.
  • The specified “unmodifiableMap()” method of the “Collections” class gives an unmodifiable map view in return.
  • Likewise, in main, access the “static” map storing the values of the static method i.e., “actualMap()”.

Output

Here, it can be indicated that the “static” map is returned appropriately.

Conclusion

A “static” map can be accessed without instantiating the class and can be set up/initialized in Java with the help of the “Static Initialization block”, or via the “Static” method. This article demonstrated the approaches to setting up a “static” map in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.