10

Is there a more streamlined way to do the following?

Map<String, String> map = new HashMap<String, String>();
map.put("a", "apple");
map.put("b", "bear");
map.put("c", "cat");

I'm looking for something closer to this.

 Map<String, String> map = MapBuilder.build("a", "apple", "b", "bear", "c", "cat");
3

5 Answers 5

16

There's always double-brace initialization:

Map<String, String> map = new HashMap<String, String>(){{
    put("a", "apple"); put("b", "bear"); put("c", "cat");}};

There are problems with this approach. It returns an anonymous inner class extending HashMap, not a HashMap. If you need to serialize the map then know that serialization of inner classes is discouraged.

6
13

No, there isn't, but I wrote a method to do exactly this, inspired by Objective-C NSDictionary class:

public static Map<String, Object> mapWithKeysAndObjects(Object... objects) {

    if (objects.length % 2 != 0) {
        throw new IllegalArgumentException(
                "The array has to be of an even size - size is "
                        + objects.length);
    }

    Map<String, Object> values = new HashMap<String, Object>();

    for (int x = 0; x < objects.length; x+=2) {
      values.put((String) objects[x], objects[x + 1]);
    }

    return values;

}
7
  • 3
    This breaks Java generics type safety. Jul 26, 2011 at 17:24
  • 2
    I don't see why you would want to reinvent ImmutableMap.of(...) in google's collection Jul 26, 2011 at 17:27
  • 7
    I would reinvent to avoid adding yet another unnecessary library full of unnecessary classes when all i want is a single method. Jul 26, 2011 at 17:29
  • Great idea @Robert, added it now. Jul 26, 2011 at 17:31
  • 1
    Guava is bloated v18 adds 2.5MB jar file (without sources), if you are going to use 1% of it, I'd rather use custom sollutions like this one for faster compile/deploy times. Nov 20, 2014 at 8:41
9

You could use ImmutableMap.Builder from Google collections library.

0
3

Java 9 adds Map.of, such as:

Map<String, String> map = Map.of("a", "apple", "b", "bear", "c", "cat");

Up to 10 entries are supported. For more entries you can use the overload taking Entry:

Map<String, String> map 
    = Map.ofEntries
        (Map.entry("a", "apple")
        , Map.entry("b", "bear")
        , Map.entry("c", "cat"));

Note that these methods do not return a HashMap. It returns an optimized immutable map.

2

You could always use double brace initialization:

Map<String, String> map = new HashMap<String, String>() {{
    put("foo", "bar");
    put("baz", "qux");
}}

But bear in mind this might not be efficient according to these answers.

1
  • 1
    whoever came up with the name 'double brace initialization' should be shot. This is a valid declaration of an anonymous class combined with a non-static initializer block. Jul 26, 2011 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.