How to initialize a static map in Java



This example shows how to initialize a static map. It actually contains two examples, the first one can be used with any version of Java and the second one can be used from Java 8 and forward.

In the example below we initialize two static maps in two different ways. The first map is initialized with a static code block, or a static initializer. The second static map is initialized using an instance initializer. Finally the contents of both static maps are printed out in the main method.

The output from the code above is:

Using Java 8 features to initialize a static map

The second example also initialize a static map, but with lambda expressions:

The output from the code above:







Search for more Java info on this site here:
Custom Search


Please type any questions here.