10

I want to declare a fully populated map field in a single statement, (which may contain several nested statements,) like this:

private static final Map<Integer,Boolean> map = 
    something-returning-an-unmodifiable-fully-populated-HashMap;

Anonymous initializers won't do, for the same reason that invoking a function which returns a new populated map won't do: they require two top-level statements: one for the variable declaration, and one for the method or initializer.

The double curly bracket ({{ and }}) idiom will work, but it creates a whole new class which extends HashMap<>, and I do not like the overhead represented by this.

Do the lambdas of Java 8 perhaps offer a better way of accomplishing this?

5
  • 2
    Is there any rationale reason for this? For a Map, it doesn’t matter whether you write Type variable = expression; or Type variable; { variable = expression; }. It will create exactly the same code.
    – Holger
    Sep 30, 2015 at 15:11
  • The two separate statements are not syntactically bound together, so they may be separated by copy-paste or by accidentally inserting something else between them. I want to keep it all in one statement for the nice snug feeling that single statements give.
    – Mike Nakis
    Sep 30, 2015 at 16:43
  • 1
    Even if there are some statements inserted in-between, it doesn’t break nor change the semantics. And for final fields, the compiler will immediately shout, if someone removes the initializer or adds a second. However, if you think that’s too fragile, there is still nothing wrong with using a method. Then, the invocation is bound to the variable, when being used as an initializer, while the method should be named after what it creates, thus doesn’t need to be bound to the variable which uses it.
    – Holger
    Sep 30, 2015 at 16:47
  • @Holger true on all accounts. But suppose we want that nice snug feeling of a single statement.
    – Mike Nakis
    Sep 30, 2015 at 17:32
  • 2
    Well, you have some solutions. Though I’m not sure whether they create a nice snug feeling…
    – Holger
    Sep 30, 2015 at 17:35

4 Answers 4

12

If you want to initialize a Map in a single statement, you can use Collectors.toMap.

Imagine you want to build a Map<Integer, Boolean> mapping an integer to the result of calling some function f:

private static final Map<Integer,Boolean> MAP = 
        Collections.unmodifiableMap(IntStream.range(0, 1000)
                                             .boxed()
                                             .collect(Collectors.toMap(i -> i, i -> f(i))));

private static final boolean f(int i) {
    return Math.random() * 100 > i;
}

If you want to initialize it with "static" known values, like the example in your answer, you can abuse the Stream API like this:

private static final Map<Integer, Boolean> MAP = 
       Stream.of(new Object[] { 1, false }, new Object[] { 2, true })
             .collect(Collectors.toMap(s -> (int) s[0], s -> (boolean) s[1]));

Note that this is a real abuse and I personally would never use it: if you want to construct a Map with known static values, there is nothing to gain from using Streams and you would be better off to use a static initializer.

9
  • 1
    Good solution, however, this only works if the values can be computed. What if it's an enumerated list of arbituary values, which needs to be mapped to a key (not an integer)?
    – Chii
    Sep 30, 2015 at 14:35
  • I was about to ask the same question as @Chii.
    – Mike Nakis
    Sep 30, 2015 at 14:36
  • 1
    @MikeNakis Edited but I'm not proud of it
    – Tunaki
    Sep 30, 2015 at 14:56
  • 5
    Yeah, now that we have a solution, we may search for a problem that it could solve…
    – Holger
    Sep 30, 2015 at 15:16
  • 1
    @MikeNakis Java 9 provided factory methods for maps, not the “initialize a map using a lambda” that you asked for, which was far away from a one-liner. As I wrote in this comment seven years ago “there is still nothing wrong with using a method” and that’s precisely what Java 9 gave you, a method. No overcomplicating lambda stuff.
    – Holger
    Nov 14, 2022 at 13:15
4

Google Guava's Maps class provides some nice utility methods for this. Additionally, there is the ImmutableMap class and its static methods. Have a look at:

    ImmutableMap.of(key1, value1, key2, value2, ...);
    Maps.uniqueIndex(values, keyExtractor);
    Maps.toMap(keys, valueMapper);
4

If you really want to do initialize the map in single statement, you can write your custom builder and use it in your project. Something like this:

public class MapBuilder<K, V> {
    private final Map<K, V> map;

    private MapBuilder(Map<K, V> map) {
        this.map = map;
    }

    public MapBuilder<K, V> put(K key, V value) {
        if(map == null)
            throw new IllegalStateException();
        map.put(key, value);
        return this;
    }

    public MapBuilder<K, V> put(Map<? extends K, ? extends V> other) {
        if(map == null)
            throw new IllegalStateException();
        map.putAll(other);
        return this;
    }

    public Map<K, V> build() {
        Map<K, V> m = map;
        map = null;
        return Collections.unmodifiableMap(m);
    }

    public static <K, V> MapBuilder<K, V> unordered() {
        return new MapBuilder<>(new HashMap<>());
    }

    public static <K, V> MapBuilder<K, V> ordered() {
        return new MapBuilder<>(new LinkedHashMap<>());
    }

    public static <K extends Comparable<K>, V> MapBuilder<K, V> sorted() {
        return new MapBuilder<>(new TreeMap<>());
    }

    public static <K, V> MapBuilder<K, V> sorted(Comparator<K> comparator) {
        return new MapBuilder<>(new TreeMap<>(comparator));
    }
}

Usage example:

Map<Integer, Boolean> map = MapBuilder.<Integer, Boolean>unordered()
    .put(1, true).put(2, false).build();

This works in Java-7 as well.

As a side note, we will likely to see something like Map.of(1, true, 2, false) in Java-9. See JDK-8048330 for details.

5
  • You should really check for null in the constructor, rather than in every put
    – Alexander
    Jul 10, 2017 at 20:56
  • @Alexander, no. Null-check is necessary to prevent using the builder after .build() is called. Null-check in constructor is useless for this. As constructor is private, we fully control that null is not passed to it. Jul 12, 2017 at 3:59
  • "Null-check is necessary to prevent using the builder after .build() is called" Why limit to only building once? I can imagine scenarios (particularly for testing) where I would want to build a series of maps, each with one new k/v pair added to the previous
    – Alexander
    Jul 12, 2017 at 4:01
  • @Alexander, as this builder creates mutable Maps, newly-added elements after a single Map is created will modify Map previously returned by build() call. You will need an additional code to copy Map when builder is used after build(), and this should not degrade the performance of common case when only one Map is necessary (i.e. you cannot just copy Map inside build()). This is doable, but somewhat more complex and was unnecessary for this answer. Jul 14, 2017 at 5:21
  • Ah. I made two key mistakes: one, I forgot that Collections.unmodifiableMap is just a wrapper, which doesn't perform a copy. Second, I've been getting spoiled by my recent dive into Swift, where Dictionaries are value types with copy-on-write
    – Alexander
    Jul 14, 2017 at 5:34
4

Here is how to implement a field initializer in Java 8 in a single statement using a lambda.

private static final Map<Integer,Boolean> map =
        ((Supplier<Map<Integer,Boolean>>)() -> {
            Map<Integer,Boolean> mutableMap = new HashMap<>();
            mutableMap.put( 1, false );
            mutableMap.put( 2, true );
            return Collections.unmodifiableMap( mutableMap );
        }).get();

Java 9 solution:

private static final Map<Integer,Boolean> map = Map.of( 1, false, 2, true );

and if you have more than 10 entries, Map.of() won't work, so you need this:

private static final Map<Integer,Boolean> map = Map.ofEntries( 
        Map.entry( 1, false ), 
        Map.entry( 2, true ), 
        Map.entry( 3, false ), 
        Map.entry( 4, true ), 
        Map.entry( 5, false ), 
        Map.entry( 6, true ), 
        Map.entry( 7, false ), 
        Map.entry( 8, true ), 
        Map.entry( 9, false ), 
        Map.entry( 10, true ), 
        Map.entry( 11, false ) );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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