Initializing a static Map in Java means creating and populating a Map that belongs to the class (shared across all objects) and is loaded when the class is initialized.
- Used for constant or reusable data
- Improves performance by avoiding repeated initialization
- Common in configurations, lookups, and caching
1. Using Static Block
Used to initialize a static map using a separate block executed at class loading time.
import java.util.*;
public class Geeks{
static Map<Integer, String> map;
static {
map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Spring");
map.put(3, "Hibernate");
}
public static void main(String[] args) {
System.out.println(map);
}
}
Output
{1=Java, 2=Spring, 3=Hibernate}
Explanation: A static block allows you to write multiple statements for initializing the map cleanly. It is executed once when the class is loaded, making it suitable for complex setups.
2. Using Map.of()
Creates a small, immutable static map in a concise way.
import java.util.*;
public class Geeks{
static Map<Integer, String> map = Map.of(
1, "Java",
2, "Spring",
3, "Hibernate"
);
public static void main(String[] args) {
System.out.println(map);
}
}
Output
{2=Spring, 3=Hibernate, 1=Java}
Explanation: Map.of() is best for initializing maps with a few fixed key-value pairs. The created map is immutable, so entries cannot be modified after creation.
3. Using Map.ofEntries()
Used to create larger immutable maps with multiple entries.
import java.util.*;
public class Geeks{
static Map<Integer, String> map = Map.ofEntries(
Map.entry(1, "Java"),
Map.entry(2, "Spring"),
Map.entry(3, "Hibernate"),
Map.entry(4, "Microservices")
);
public static void main(String[] args) {
System.out.println(map);
}
}
Output
{4=Microservices, 3=Hibernate, 2=Spring, 1=Java}
Explanation: Map.ofEntries() overcomes the entry limit of Map.of() and supports more elements. It also creates an immutable map, ensuring data safety.
4. Using Double Brace Initialization
Initializes a map using an anonymous inner class.
import java.util.*;
public class Geeks{
static Map<Integer, String> map = new HashMap<>() {{
put(1, "Java");
put(2, "Spring");
put(3, "Hibernate");
}};
public static void main(String[] args) {
System.out.println(map);
}
}
Output
{1=Java, 2=Spring, 3=Hibernate}
Explanation: It allows inline initialization using double braces, making code look compact. However, it creates extra objects and should be avoided in production code.
5. Using Java 8 Stream
Initializes a map dynamically using streams and collectors.
import java.util.*;
import java.util.stream.*;
public class Geeks{
static Map<Integer, String> map =
Stream.of(new Object[][] {
{1, "Java"},
{2, "Spring"},
{3, "Hibernate"}
}).collect(Collectors.toMap(
data -> (Integer) data[0],
data -> (String) data[1]
));
public static void main(String[] args) {
System.out.println(map);
}
}
Output
{1=Java, 2=Spring, 3=Hibernate}
Explanation: Streams allow building maps from arrays or computed data efficiently. This approach is useful when data needs transformation during initialization.
When to Use Which Method?
| Method | Best Use Case |
|---|---|
| Static Block | Complex initialization |
| Map.of() | Small, fixed data |
| Map.ofEntries() | Larger immutable data |
| Double Brace | Avoid in production |
| Streams | Dynamic or computed data |