By dogbane

2009-02-03 15:41:33 8 Comments

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating the two methods:

import java.util.HashMap;
import java.util.Map;

public class Test {
    private static final Map<Integer, String> myMap = new HashMap<>();
    static {
        myMap.put(1, "one");
        myMap.put(2, "two");

    private static final Map<Integer, String> myMap2 = new HashMap<>(){
            put(1, "one");
            put(2, "two");


@Peter Štibraný 2009-02-03 21:40:14

I would use:

public class Test {
    private static final Map<Integer, String> MY_MAP = createMap();

    private static Map<Integer, String> createMap() {
        Map<Integer, String> result = new HashMap<>();
        result.put(1, "one");
        result.put(2, "two");
        return Collections.unmodifiableMap(result);
  1. it avoids an anonymous class, which I personally consider to be a bad style, and avoid
  2. it makes the creation of map more explicit
  3. it makes map unmodifiable
  4. as MY_MAP is constant, I would name it like constant

@Jonik 2013-12-29 14:31:55

Of the pure JDK options (no libs), I like this the most, because map definition is clearly linked to its initialisation. Also agreed on constant naming.

@romulusnr 2020-04-21 02:09:42

It never occurred to me that you could do this.

@Jonik 2011-08-31 13:58:37

I like the Guava way of initialising a static, immutable map:

static final Map<Integer, String> MY_MAP = ImmutableMap.of(
    1, "one",
    2, "two"

As you can see, it's very concise (because of the convenient factory methods in ImmutableMap).

If you want the map to have more than 5 entries, you can no longer use ImmutableMap.of(). Instead, try ImmutableMap.builder() along these lines:

static final Map<Integer, String> MY_MAP = ImmutableMap.<Integer, String>builder()
    .put(1, "one")
    .put(2, "two")
    // ... 
    .put(15, "fifteen")

To learn more about the benefits of Guava's immutable collection utilities, see Immutable Collections Explained in Guava User Guide.

(A subset of) Guava used to be called Google Collections. If you aren't using this library in your Java project yet, I strongly recommend trying it out! Guava has quickly become one of the most popular and useful free 3rd party libs for Java, as fellow SO users agree. (If you are new to it, there are some excellent learning resources behind that link.)

Update (2015): As for Java 8, well, I would still use the Guava approach because it is way cleaner than anything else. If you don't want Guava dependency, consider a plain old init method. The hack with two-dimensional array and Stream API is pretty ugly if you ask me, and gets uglier if you need to create a Map whose keys and values are not the same type (like Map<Integer, String> in the question).

As for future of Guava in general, with regards to Java 8, Louis Wasserman said this back in 2014, and [update] in 2016 it was announced that Guava 21 will require and properly support Java 8.

Update (2016): As Tagir Valeev points out, Java 9 will finally make this clean to do using nothing but pure JDK, by adding convenience factory methods for collections:

static final Map<Integer, String> MY_MAP = Map.of(
    1, "one", 
    2, "two"

@Jonik 2012-01-27 10:11:37

Seems like our fellow SO admins have deleted the venerable "Most useful free third party Java libraries" question to which I linked. :( Damn them.

@crunchdog 2012-05-03 07:29:38

I agree, this is the nicest way of initializing a constant map. Not only more readable but also since Collections.unmodifiableMap returns a read-only view of the underlying map (that still can be modified).

@Jonik 2013-02-27 15:06:49

I can now see deleted questions (with 10k+ rep), so here's a copy of 'Most useful free third-party Java libraries'. It's only the first page, but at least you can find the Guava resources mentioned above.

@Wrench 2017-03-20 14:39:21

I really prefer this approach, although it's beneficial to know how to do it without extra dependencies.

@cybersoft 2017-06-06 07:59:24

JEP 186 still not closed, so it may introduce new features related to collection literals

@Terran 2017-12-28 13:50:12

Instead of lazily using unmodifiable maps (which are mostlikely "stringly-typed"), consider creating a proper Class or Enum / EnumSet.

@Vectorjohn 2018-01-15 19:29:44

My only complaint with the ImmutableMap.of(...) approach is it can't possibly be type safe, unless the library specifically hard-coded static methods with varying length arguments. Which I guess is why it only worked up to 5 entries. It looks like Java 9 will have that same limitation with the convenience factory methods (but maybe more, e.g. 10 entries). However they mention static factory methods for Map.Entry so you can do arbitrary length: Map.ofEntries(entry(k1, v1), ...)

@mid 2018-01-24 14:25:16

The Java 9 method is clean and all, until you realize how it was implemented

@Koushik Roy 2020-03-04 14:48:59

it doesn't work for null values

@Domingo Ignacio 2014-04-16 07:32:03

Now that Java 8 is out, this question warrants revisiting. I took a stab at it -- looks like maybe you can exploit lambda expression syntax to get a pretty nice and concise (but type-safe) map literal syntax that looks like this:

Map<String,Object> myMap = hashMap(
    bob -> 5,
    TheGimp -> 8,
    incredibleKoolAid -> "James Taylor",
    heyArnold -> new Date()

Map<String,Integer> typesafeMap = treeMap(
    a -> 5,
    bee -> 8,
    sea -> 13
    deep -> 21

Untested sample code at Would be curious about the opinions of others on this (it's mildly evil...)

@Domingo Ignacio 2014-05-07 20:21:57

Note: Turns out, after actually trying it later... the above does not actually work. I tried it with a few different javac flags and was unable to get the name used for the parameter to be retained.

@akhil_mittal 2016-05-23 07:17:08

Java 9

We can use Map.ofEntries, calling Map.entry( k , v ) to create each entry.

import static java.util.Map.entry;
private static final Map<Integer,String> map = Map.ofEntries(
        entry(1, "one"),
        entry(2, "two"),
        entry(3, "three"),
        entry(4, "four"),
        entry(5, "five"),
        entry(6, "six"),
        entry(7, "seven"),
        entry(8, "eight"),
        entry(9, "nine"),
        entry(10, "ten"));

We can also use Map.of as suggested by Tagir in his answer here but we cannot have more than 10 entries using Map.of.

Java 8 (Neat Solution)

We can create a Stream of map entries. We already have two implementations of Entry in java.util.AbstractMap which are SimpleEntry and SimpleImmutableEntry. For this example we can make use of former as:

import java.util.AbstractMap.*;
private static final Map<Integer, String> myMap = Stream.of(
            new SimpleEntry<>(1, "one"),
            new SimpleEntry<>(2, "two"),
            new SimpleEntry<>(3, "three"),
            new SimpleEntry<>(4, "four"),
            new SimpleEntry<>(5, "five"),
            new SimpleEntry<>(6, "six"),
            new SimpleEntry<>(7, "seven"),
            new SimpleEntry<>(8, "eight"),
            new SimpleEntry<>(9, "nine"),
            new SimpleEntry<>(10, "ten"))
            .collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));

@Danon 2019-03-20 16:14:03

The new SimpleEntry<>() way is far less readable than static put() :/

@Tagir Valeev 2015-12-29 10:07:24

Map.of in Java 9+

private static final Map<Integer, String> MY_MAP = Map.of(1, "one", 2, "two");

See JEP 269 for details. JDK 9 reached general availability in September 2017.

@ZhekaKozlov 2017-05-11 04:26:02

Or if you want more than 10 key-value pairs, you can use Map.ofEntries

@mid 2018-01-24 14:24:39

This is clean and all, until you realize how it was implemented

@Somaiah Kumbera 2018-05-28 14:21:31

Ugh thats so sad - looks like it only supports 10 entries, after which you need to use ofEntries. Lame.

@vikingsteve 2019-01-31 14:54:16

The implementation cleanliness in the JDK shouldn't matter as long as it works and satisfies the contract. Like any black box, implementation details can always be fixed in future if really needed...

@Luke Hutchison 2019-10-31 00:44:59

@mid That's the only typesafe way to do this in Java.

@mid 2019-10-31 07:25:34

@LukeHutchison I wouldn't say a library has to be clean internally.

@Luke Hutchison 2019-11-01 02:42:50

@mid I agree. Well the maximum number of parameters in a Java method is 255, so technically you could only go as high as 128 key-value pairs in this way, while supporting type safety. The real problem here is that arrays are not able to be made generic, so you can't declare a varargs type like Map.of(Entry<Integer, String>...).

@Basil Bourque 2019-12-17 03:10:24

For more than 10 entries, see this Answer on this same page, using Map.ofEntries.

@GeroldBroser reinstates Monica 2018-09-10 23:59:34

Note: This answer actually belongs to question How to directly initialize a HashMap (in a literal way)? but since that's marked as duplicate of this one...

Prior to Java 9 with its Map.of() (which is also limited to 10 mappings) you can extend a Map implementation of your choice, e.g.:

public class InitHashMap<K, V> extends HashMap<K, V>

re-implement HashMap's constructors:

public InitHashMap() {

public InitHashMap( int initialCapacity, float loadFactor ) {
    super( initialCapacity, loadFactor );

public InitHashMap( int initialCapacity ) {
    super( initialCapacity );

public InitHashMap( Map<? extends K, ? extends V> map ) {
    super( map );

and add an additional constructor that's inspired by Aerthel's answer but is generic by using Object... and <K, V> types:

public InitHashMap( final Object... keyValuePairs ) {

    if ( keyValuePairs.length >>> 1 != 0 )
        throw new IllegalArgumentException( "Uneven number of arguments." );

    K key = null;
    int i = -1;

    for ( final Object keyOrValue : keyValuePairs )
        switch ( ++i >>> 1 ) {
            case 0:  // key
                if ( keyOrValue == null )
                    throw new IllegalArgumentException( "Key[" + (i >>> 1) + "] is <null>." );
                key = (K) keyOrValue;
            case 1:  // value
                put( key, (V) keyOrValue );


public static void main( final String[] args ) {

    final Map<Integer, String> map = new InitHashMap<>( 1, "First", 2, "Second", 3, "Third" );
    System.out.println( map );


{1=First, 2=Second, 3=Third}

You also can extend the Map interface likewise:

public interface InitMap<K, V> extends Map<K, V> {

    static <K, V> Map<K, V> of( final Object... keyValuePairs ) {

        if ( keyValuePairs.length >>> 1 != 0 )
            throw new IllegalArgumentException( "Uneven number of arguments." );

        final Map<K, V> map = new HashMap<>( keyValuePairs.length >>> 1, .75f );
        K key = null;
        int i = -1;

        for ( final Object keyOrValue : keyValuePairs )
            switch ( ++i >>> 1 ) {
                case 0: // key
                    if ( keyOrValue == null )
                        throw new IllegalArgumentException( "Key[" + (i >>> 1) + "] is <null>." );
                    key = (K) keyOrValue;
                case 1: // value
                    map.put( key, (V) keyOrValue );
        return map;


public static void main( final String[] args ) {

    System.out.println( InitMap.of( 1, "First", 2, "Second", 3, "Third" ) );


{1=First, 2=Second, 3=Third}

@Frontear 2019-10-18 00:56:23

Any reason why you chose to do >>> 1 instead of % 2, apart from making it intentionally confusing?

@GeroldBroser reinstates Monica 2019-10-18 07:47:29

@Frontear There are even more than one: 1) If the compiler/JIT doesn't optimize % 2 accordingly–which I don't know–shift operations are much more performant than math operations since they are implemented hard-wired in any CPU I'm aware of. 2) There are only rare cases where shift operations can be used in high-level languages, so I use them whenever I can to remind myself– and others who read my code–that they exist. 3) It increases the lifetime of the other operators due to reduced wearout [just kidding, of course, but I wanted it to be three :]

@Frontear 2019-10-18 11:59:04

Sounds like something that the compiler should be caring for, no? Adding shift operations into places where a more simplified operation could be shown just leads to misunderstood code.

@GeroldBroser reinstates Monica 2019-10-18 22:15:00

@Frontear SHOULD is not MUST. Shift operations are way simpler than math ops (from a tech POV behind the scenes where 0, 1 and their positions in a combination thereof matter). I agree, they aren't used in high-level language programs that often. Have you ever coded in ASM or machine code? I have and therefore these operations are very familiar to me and I use them if there's the chance to do so. It's OK for me if you don't like and dont' use them. I can live with both. Are you trying to convince me to jettison my knowledge because others don't have that?

@egridasov 2019-06-25 08:39:48

This one uses Apache commons-lang which will most likely be on your class path already:

Map<String, String> collect = Stream.of(
        Pair.of("hello", "world"),
        Pair.of("abc", "123"),
        Pair.of("java", "eight")
).collect(Collectors.toMap(Pair::getKey, Pair::getValue));

@Miserable Variable 2009-02-03 15:51:04

The instance initialiser is just syntactic sugar in this case, right? I don't see why you need an extra anonymous class just to initialize. And it won't work if the class being created is final.

You can create an immutable map using a static initialiser too:

public class Test {
    private static final Map<Integer, String> myMap;
    static {
        Map<Integer, String> aMap = ....;
        aMap.put(1, "one");
        aMap.put(2, "two");
        myMap = Collections.unmodifiableMap(aMap);

@jasonmp85 2010-06-03 08:22:09

This is the idiom I've used for years and I've never had anyone bat an eye at it. I do the same for unmodifiable constant Sets and Lists too.

@Luke 2011-05-04 14:36:50

How would I handle a HashMap<String, String> with a String key. The Map object doesn't allow me to have a String key so I can't use unmodifiableMap(). I guess casting to a HashMap would defeat the purpose as well. Any ideas?

@mluisbrown 2011-05-17 10:18:58

@Luke of course the Map object allows you to use a String key. I've used the above idiom many times with Map<String, String>.

@Luke 2011-05-18 04:14:28

@mluisbrown: Maybe it is an Android limitation (I forgot to mention that), as it won't let me use a String key for a Map object in an Android app.

@mluisbrown 2011-05-25 17:38:20

@Luke I seriously doubt that Android has such a limitation. It makes no sense at all. A quick search found this question here (and many others) which seems to imply you can use a String key for a Map object in Android.

@Jordan 2012-01-03 22:13:54

So no one else bothers to investigate, I can confirm there's no problem with using a String key for a Map object on Android.

@Miserable Variable 2012-01-03 23:53:57

Jordan: it is an old topic now but I suspect @Luke was trying to use a string as a key in a map that had a different key type, e.g. Map<Integer, String>.

@begin 2018-05-16 10:20:13

what is use of creating map inside static block, static reference created outside of static block can be used? could you please explain?

@Miserable Variable 2018-05-17 20:19:29

In this case, the reason for creating a local map inside the static block is to eventually convert it to an unmodifiableMap, so that Test.myMap is unmodifiable. I do not understand what you mean by "eference created outside of static block can be used"

@Donald Raab 2012-12-18 23:10:43

With Eclipse Collections, all of the following will work:

import java.util.Map;

import org.eclipse.collections.impl.factory.Maps;

public class StaticMapsTest
    private static final Map<Integer, String> MAP =
        Maps.mutable.with(1, "one", 2, "two");

    private static final MutableMap<Integer, String> MUTABLE_MAP =
       Maps.mutable.with(1, "one", 2, "two");

    private static final MutableMap<Integer, String> UNMODIFIABLE_MAP =
        Maps.mutable.with(1, "one", 2, "two").asUnmodifiable();

    private static final MutableMap<Integer, String> SYNCHRONIZED_MAP =
        Maps.mutable.with(1, "one", 2, "two").asSynchronized();

    private static final ImmutableMap<Integer, String> IMMUTABLE_MAP =
        Maps.mutable.with(1, "one", 2, "two").toImmutable();

    private static final ImmutableMap<Integer, String> IMMUTABLE_MAP2 =
        Maps.immutable.with(1, "one", 2, "two");

You can also statically initialize primitive maps with Eclipse Collections.

import org.eclipse.collections.impl.factory.primitive.IntObjectMaps;

public class StaticPrimitiveMapsTest
    private static final MutableIntObjectMap<String> MUTABLE_INT_OBJ_MAP =
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two");

    private static final MutableIntObjectMap<String> UNMODIFIABLE_INT_OBJ_MAP =
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two")

    private static final MutableIntObjectMap<String> SYNCHRONIZED_INT_OBJ_MAP =
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two")

    private static final ImmutableIntObjectMap<String> IMMUTABLE_INT_OBJ_MAP =
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two")

    private static final ImmutableIntObjectMap<String> IMMUTABLE_INT_OBJ_MAP2 =
                    .newWithKeyValue(1, "one")
                    .newWithKeyValue(2, "two");

Note: I am a committer for Eclipse Collections

@Kenny Cason 2019-07-10 21:05:34

I really wish Eclipse Collections was the default collections library for Java. I enjoy it so much more than Guava + JCL.

@user_3380739 2016-11-28 23:17:00

Here is the code by AbacusUtil

Map<Integer, String> map = N.asMap(1, "one", 2, "two");
// Or for Immutable map 
ImmutableMap<Integer, String> = ImmutableMap.of(1, "one", 2, "two");

Declaration: I'm the developer of AbacusUtil.

@Vlasec 2015-05-20 09:13:47

I've read the answers and i decided to write my own map builder. Feel free to copy-paste and enjoy.

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

 * A tool for easy creation of a map. Code example:<br/>
 * {@code MapBuilder.of("name", "Forrest").and("surname", "Gump").build()}
 * @param <K> key type (inferred by constructor)
 * @param <V> value type (inferred by constructor)
 * @author Vlasec (for
public class MapBuilder <K, V> {
    private Map<K, V> map = new HashMap<>();

    /** Constructor that also enters the first entry. */
    private MapBuilder(K key, V value) {
        and(key, value);

    /** Factory method that creates the builder and enters the first entry. */
    public static <A, B> MapBuilder<A, B> mapOf(A key, B value) {
        return new MapBuilder<>(key, value);

    /** Puts the key-value pair to the map and returns itself for method chaining */
    public MapBuilder<K, V> and(K key, V value) {
        map.put(key, value);
        return this;

     * If no reference to builder is kept and both the key and value types are immutable,
     * the resulting map is immutable.
     * @return contents of MapBuilder as an unmodifiable map.
    public Map<K, V> build() {
        return Collections.unmodifiableMap(map);

EDIT: Lately, I keep finding public static method of pretty often and I kinda like it. I added it into the code and made the constructor private, thus switching to static factory method pattern.

EDIT2: Even more recently, I no longer like static method called of, as it looks pretty bad when using static imports. I renamed it to mapOf instead, making it more suitable for static imports.

@Outlaw Programmer 2009-02-03 15:44:58

One advantage to the second method is that you can wrap it with Collections.unmodifiableMap() to guarantee that nothing is going to update the collection later:

private static final Map<Integer, String> CONSTANT_MAP = 
    Collections.unmodifiableMap(new HashMap<Integer, String>() {{ 
        put(1, "one");
        put(2, "two");

 // later on...

 CONSTANT_MAP.put(3, "three"); // going to throw an exception!

@Patrick 2009-02-03 20:30:03

Can't you easily do this in the first method by moving the new operator into the static {} block and wrapping it?

@Tom Hawtin - tackline 2009-02-03 21:25:23

I'd move the constructor call into the static initialised anyway. Anything else just looks odd.

@Kip 2009-12-10 15:49:22

any idea what performance hit there might be from using an anonymous class as opposed to a concrete class?

@Luke Hutchison 2014-09-14 00:44:37

Here's a Java 8 one-line static map initializer:

private static final Map<String, String> EXTENSION_TO_MIMETYPE = String[][] {
        { "txt", "text/plain" }, 
        { "html", "text/html" }, 
        { "js", "application/javascript" },
        { "css", "text/css" },
        { "xml", "application/xml" },
        { "png", "image/png" }, 
        { "gif", "image/gif" }, 
        { "jpg", "image/jpeg" },
        { "jpeg", "image/jpeg" }, 
        { "svg", "image/svg+xml" },
    }).collect(Collectors.toMap(kv -> kv[0], kv -> kv[1]));

Edit: to initialize a Map<Integer, String> as in the question, you'd need something like this:

static final Map<Integer, String> MY_MAP = Object[][]{
        {1, "one"},
        {2, "two"},
}).collect(Collectors.toMap(kv -> (Integer) kv[0], kv -> (String) kv[1]));

Edit(2): There is a better, mixed-type-capable version by i_am_zero that uses a stream of new SimpleEntry<>(k, v) calls. Check out that answer:

@Jonik 2015-12-08 22:51:12

I took the liberty to add a version that is equivalent with the question and other answers: init a Map whose keys and values are of different type (so String[][] won't do, Object[][] is needed). IMHO, this approach is ugly (even more so with the casts) and hard to remember; wouldn't use it myself.

@R. Oosterholt 2017-05-23 09:29:22

Your second approach (Double Brace initialization) is thought to be an anti pattern, so I would go for the first approach.

Another easy way to initialise a static Map is by using this utility function:

public static <K, V> Map<K, V> mapOf(Object... keyValues) {
    Map<K, V> map = new HashMap<>(keyValues.length / 2);

    for (int index = 0; index < keyValues.length / 2; index++) {
        map.put((K)keyValues[index * 2], (V)keyValues[index * 2 + 1]);

    return map;

Map<Integer, String> map1 = mapOf(1, "value1", 2, "value2");
Map<String, String> map2 = mapOf("key1", "value1", "key2", "value2");

Note: in Java 9 you can use Map.of

@Bharanidharan K 2017-11-26 03:38:36

If you want unmodifiable map, finally java 9 added a cool factory method of to Map interface. Similar method is added to Set, List as well.

Map<String, String> unmodifiableMap = Map.of("key1", "value1", "key2", "value2");

@jglatre 2017-10-25 16:28:13

Well... I like enums ;)

enum MyEnum {
    ONE   (1, "one"),
    TWO   (2, "two"),
    THREE (3, "three");

    int value;
    String name;

    MyEnum(int value, String name) {
        this.value = value; = name;

    static final Map<Integer, String> MAP = Stream.of( values() )
            .collect( Collectors.toMap( e -> e.value, e -> ) );

@yegor256 2017-06-20 17:28:06

You may use StickyMap and MapEntry from Cactoos:

private static final Map<String, String> MAP = new StickyMap<>(
  new MapEntry<>("name", "Jeffrey"),
  new MapEntry<>("age", "35")

@neu242 2016-09-15 12:09:43

Here's my favorite when I don't want to (or cannot) use Guava's ImmutableMap.of(), or if I need a mutable Map:

public static <A> Map<String, A> asMap(Object... keysAndValues) {
    return new LinkedHashMap<String, A>() {{
        for (int i = 0; i < keysAndValues.length - 1; i++) {
            put(keysAndValues[i].toString(), (A) keysAndValues[++i]);

It's very compact, and it ignores stray values (i.e. a final key without a value).


Map<String, String> one = asMap("1stKey", "1stVal", "2ndKey", "2ndVal");
Map<String, Object> two = asMap("1stKey", Boolean.TRUE, "2ndKey", new Integer(2));

@Stromata 2017-04-30 21:28:08

If you only need to add one value to the map you can use Collections.singletonMap:

Map<K, V> map = Collections.singletonMap(key, value)

@Alex Butler 2016-11-30 01:16:09

Even with Guava's nice ImmutableMap class, sometimes I'd like to build a mutable map fluently. Finding myself wanting to avoid static blocks & the anonymous subtype thing, when Java 8 came along I wrote a tiny library to help called Fluent.

// simple usage, assuming someMap is a Map<String, String> already declared
Map<String, String> example = new Fluent.HashMap<String, String>()
    .append("key1", "val1")
    .append("key2", "val2")

With Java 8 interface defaulting I could implement the Fluent.Map methods for all standard Java Map implementations (ie HashMap, ConcurrentSkipListMap, ... etc) without tedious repetition.

Unmodifiable maps are simple too.

Map<String, Integer> immutable = new Fluent.LinkedHashMap<String, Integer>()
    .append("one", 1)
    .append("two", 2)
    .append("three", 3)

See for source, documentation and examples.

@agad 2015-09-08 09:32:09

As usual apache-commons has proper method MapUtils.putAll(Map, Object[]):

For example, to create a color map:

Map<String, String> colorMap = MapUtils.putAll(new HashMap<String, String>(), new String[][] {
     {"RED", "#FF0000"},
     {"GREEN", "#00FF00"},
     {"BLUE", "#0000FF"}

@mike rodent 2016-11-27 10:42:56

I include Apache Commons in all builds so, in the unfortunate absence of a method Arrays.asMap( ... ) in plain Java, I think this is the best solution. Reinventing the wheel is usually silly. Very slight downside is that with generics it will need an unchecked conversion.

@agad 2016-11-28 07:36:23

@mikerodent 4.1 version is generic: public static <K, V> Map<K, V> putAll(final Map<K, V> map, final Object[] array)

@mike rodent 2016-11-28 07:41:01

Tx ... yes, I'm using 4.1 but I still have to SuppressWarnings( unchecked ) in Eclipse with a line like Map<String, String> dummy = MapUtils.putAll(new HashMap<String, String>(), new Object[][]... )

@agad 2016-11-28 07:49:15

@mikerodent isn't it because of Object[][]? See updated unswear - I don't have any warning in Eclipse.

@mike rodent 2016-11-28 07:53:12

How strange... even when I go String[][] I get the "warning"! And of course that only works if your K and V are the same class. I take it you haven't (understandably) set "unchecked conversion" to "Ignore" in your Eclipse setup?

@agad 2016-11-28 08:04:57

@mikerodent I don't have annotation and "Unchecked generic type operation" is set to warning. Removing types from HashMap, causes warning to appear.

@mike rodent 2016-11-28 11:40:02

My Eclipse is Mars.2 build 4.5.2. Or could it be a Java version difference? I'm using 1.8.0_91 as my Eclipse JRE. Otherwise I give up: mystery!

@agad 2016-11-29 06:52:42

@mikerodent I have Neon Release (4.6.0) and jdk1.8.0_74

@zrvan 2015-11-20 00:26:24

With Java 8 I've come to use the following pattern:

private static final Map<String, Integer> MAP = Stream.of(
    new AbstractMap.SimpleImmutableEntry<>("key1", 1),
    new AbstractMap.SimpleImmutableEntry<>("key2", 2)
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

It's not the most terse and a bit roundabout, but

  • it doesn't require anything outside of java.util
  • it's typesafe and easily accommodates different types for key and value.

@zrvan 2015-11-20 00:28:53

if needed, one can use the toMap signature including a map supplier to specify the type of map.

@Shushant 2013-09-19 08:48:59

I like anonymous class, because it is easy to deal with it:

public static final Map<?, ?> numbers = Collections.unmodifiableMap(new HashMap<Integer, String>() {
        put(1, "some value");
                    //rest of code here

@Ali Dehghani 2016-04-10 15:08:52

JEP 269 provides some convenience factory methods for Collections API. This factory methods are not in current Java version, which is 8, but are planned for Java 9 release.

For Map there are two factory methods: of and ofEntries. Using of, you can pass alternating key/value pairs. For example, in order to create a Map like {age: 27, major: cs}:

Map<String, Object> info = Map.of("age", 27, "major", "cs");

Currently there are ten overloaded versions for of, so you can create a map containing ten key/value pairs. If you don't like this limitation or alternating key/values, you can use ofEntries:

Map<String, Object> info = Map.ofEntries(
                Map.entry("age", 27),
                Map.entry("major", "cs")

Both of and ofEntries will return an immutable Map, so you can't change their elements after construction. You can try out these features using JDK 9 Early Access.

@nazar_art 2016-02-26 21:53:35

Because Java does not support map literals, map instances must always be explicitly instantiated and populated.

Fortunately, it is possible to approximate the behavior of map literals in Java using factory methods.

For example:

public class LiteralMapFactory {

    // Creates a map from a list of entries
    public static <K, V> Map<K, V> mapOf(Map.Entry<K, V>... entries) {
        LinkedHashMap<K, V> map = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : entries) {
            map.put(entry.getKey(), entry.getValue());
        return map;
    // Creates a map entry
    public static <K, V> Map.Entry<K, V> entry(K key, V value) {
        return new AbstractMap.SimpleEntry<>(key, value);

    public static void main(String[] args) {
        System.out.println(mapOf(entry("a", 1), entry("b", 2), entry("c", 3)));


{a=1, b=2, c=3}

It is a lot more convenient than creating and populating the map an element at a time.

@Jason 2016-01-28 16:20:21

There are some good answers here, but I do want to offer one more.

Create your own static method to create and initialize a Map. I have my own CollectionUtils class in a package that I use across projects with various utilities that I use regularly that are easy for me to write and avoids the need for a dependency on some larger library.

Here's my newMap method:

public class CollectionUtils {
    public static Map newMap(Object... keyValuePairs) {
        Map map = new HashMap();
        if ( keyValuePairs.length % 2 == 1 ) throw new IllegalArgumentException("Must have even number of arguments");
        for ( int i=0; i<keyValuePairs.length; i+=2 ) {
            map.put(keyValuePairs[i], keyValuePairs[i + 1]);
        return map;


import static CollectionUtils.newMap;
// ...
Map aMap = newMap("key1", 1.23, "key2", 2.34);
Map bMap = newMap(objKey1, objVal1, objKey2, objVal2, objKey3, objVal3);
// etc...

It doesn't make use of generics, but you can typecast the map as you wish (just be sure you typecast it correctly!)

Map<String,Double> aMap = (Map<String,Double>)newMap("key1", 1.23, "key2", 2.34);

@Christian Ullenboom 2014-01-13 04:39:14

If you can use a String representation of your data this is an option too in Java 8:

static Map<Integer, String> MAP = Stream.of(
).collect(Collectors.toMap(k -> Integer.parseInt(k.split("=")[0]), v -> v.split("=")[1]));

@Tomáš Záluský 2015-08-06 16:10:30

In Java 8, procedural approach can also be wrapped in Supplier:

Map<String,String> m = ((Supplier<Map<String,String>>)(() -> {
    Map<String,String> result = new HashMap<>();
    return result;

It's only hypothetical way but can come handy if you really need one-liner.

@josh 2015-06-03 20:46:29

I have not seen the approach I use (and have grown to like) posted in any answers, so here it is:

I don't like using static initializers because they are clunky, and I don't like anonymous classes because it is creating a new class for each instance.

instead, I prefer initialization that looks like this:

    entry("keyA", "val1"),
    entry("keyB", "val2"),
    entry("keyC", "val3")

unfortunately, these methods are not part of the standard Java library, so you will need to create (or use) a utility library that defines the following methods:

 public static <K,V> Map<K,V> map(Map.Entry<K, ? extends V>... entries)
 public static <K,V> Map.Entry<K,V> entry(K key, V val)

(you can use 'import static' to avoid needing to prefix the method's name)

I found it useful to provide similar static methods for the other collections (list, set, sortedSet, sortedMap, etc.)

Its not quite as nice as json object initialization, but it's a step in that direction, as far as readability is concerned.

@Jim Daehn 2015-01-28 21:35:25

I like using the static initializer "technique" when I have a concrete realization of an abstract class that has defined an initializing constructor but no default constructor but I want my subclass to have a default constructor.

For example:

public abstract class Shape {

    public static final String COLOR_KEY = "color_key";
    public static final String OPAQUE_KEY = "opaque_key";

    private final String color;
    private final Boolean opaque;

     * Initializing constructor - note no default constructor.
     * @param properties a collection of Shape properties
    public Shape(Map<String, Object> properties) {
        color = ((String) properties.getOrDefault(COLOR_KEY, "black"));
        opaque = (Boolean) properties.getOrDefault(OPAQUE_KEY, false);

     * Color property accessor method.
     * @return the color of this Shape
    public String getColor() {
        return color;

     * Opaque property accessor method.
     * @return true if this Shape is opaque, false otherwise
    public Boolean isOpaque() {
        return opaque;

and my concrete realization of this class -- but it wants/needs a default constructor:

public class SquareShapeImpl extends Shape {

    private static final Map<String, Object> DEFAULT_PROPS = new HashMap<>();

    static {
        DEFAULT_PROPS.put(Shape.COLOR_KEY, "yellow");
        DEFAULT_PROPS.put(Shape.OPAQUE_KEY, false);

     * Default constructor -- intializes this square to be a translucent yellow
    public SquareShapeImpl() {
        // the static initializer was useful here because the call to 
        // this(...) must be the first statement in this constructor
        // i.e., we can't be mucking around and creating a map here

     * Initializing constructor -- create a Square with the given
     * collection of properties.
     * @param props a collection of properties for this SquareShapeImpl
    public SquareShapeImpl(Map<String, Object> props) {

then to use this default constructor, we simply do:

public class StaticInitDemo {

    public static void main(String[] args) {

        // create a translucent, yellow square...
        Shape defaultSquare = new SquareShapeImpl();

        // etc...

Related Questions

Sponsored Content

45 Answered Questions

[SOLVED] How do I merge two dictionaries in a single expression in Python?

16 Answered Questions

[SOLVED] How can I add new keys to a dictionary?

12 Answered Questions

[SOLVED] How to directly initialize a HashMap (in a literal way)?

57 Answered Questions

[SOLVED] How to create a memory leak in Java?

42 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

57 Answered Questions

[SOLVED] Sort a Map<Key, Value> by values

34 Answered Questions

[SOLVED] How do I sort a dictionary by value?

30 Answered Questions

24 Answered Questions

[SOLVED] Why can't I define a static method in a Java interface?

Sponsored Content