question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImmutableMap::of should accept more entries

See original GitHub issue

I’m using ImmutableMap for building structures that I can then automatically serialize to JSON, like the following adapted from http://json.org/example:

Map j = ImmutableMap.of(
        "id", id,
        "title", title,
        "debug", "on",
        "window", ImmutableMap.of(
                "title", "Window title",
                "name", "window_1",
                "width", 500,
                "height", 500
                )
        );

Coming from Python where JSON can be used in source code as-is, the above is a very nice alternative in Java land and I couldn’t think of a way to make it any better, syntax-wise. However, I hit the limit of the maximum 5 key-value pairs of ImmutableMap::of, and then I had to use rather ugly workarounds (with Builder or alternatively splitting a big Map up into two and joining them together again…).

I fully understand that there is no variable arguments overload like in ImmutableSet::of – you couldn’t guarantee at compile-time that the number of ImmutableMap::of arguments is even (to form key-value pairs), it would fail at run-time, and we don’t want that.

I also understand that there probably is some resistance in providing a high number of overloads. However, I still think the limit of 5 entries is too low. And resorting to Builder is just plain ugly in some cases. I therefore propose to raise the number of overloads to 10, as this should cover most use cases and will create less headache for users. On the implementation side it is a trivial change and I don’t see any problem.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:10
  • Comments:23 (15 by maintainers)

github_iconTop GitHub Comments

4reactions
ustaDAHcommented, May 30, 2016

@jbduncan Actually, it was just a nested types in Generics that made it clumsy, it all DOES work, my bad: Map <String, Supplier<String>> map = ImmutableMap.<String, Supplier<String>>builder() .put("one", () -> "supplier1") .build(); Having to repeat the type info is annoying but I think that’s the limitation of automatic type inference and has nothing to do with Guava.

3reactions
lowassercommented, Jun 7, 2015

I’m not sure I understand why you consider the builder approach an ugly workaround?

ImmutableMap.builder() .put(k1, v1) .put(k2, v2) … .build()

shouldn’t be that awkward.

On Sun, Jun 7, 2015 at 6:38 AM Maik Riechert notifications@github.com wrote:

I’m using ImmutableMap for building structures that I can then automatically serialize to JSON, like the following adapted from http://json.org/example:

Map j = ImmutableMap.of( “id”, id, “title”, title, “debug”, “on”, “window”, ImmutableMap.of( “title”, “Window title”, “name”, “window_1”, “width”, 500, “height”, 500 ) );

Coming from Python where JSON can be used in source code as-is, the above is a very nice alternative in Java land and I couldn’t think of a way to make it any better, syntax-wise. However, I hit the limit of the maximum 5 key-value pairs of ImmutableMap::of, and then I had to use rather ugly workarounds (with Builder or alternatively splitting a big Map up into two and joining them together again…).

I fully understand that there is no variable arguments overload like in ImmutableSet::of – you couldn’t guarantee at compile-time that the number of ImmutableMap::of arguments is even (to form key-value pairs), it would fail at run-time, and we don’t want that.

I also understand that there probably is some resistance in providing a high number of overloads. However, I still think the limit of 5 entries is too low https://stackoverflow.com/q/9489384/60982. And resorting to Builder is just plain ugly in some cases. I therefore propose to raise the number of overloads to 10, as this should cover most use cases and will create less headache for users. On the implementation side it is a trivial change and I don’t see any problem.

— Reply to this email directly or view it on GitHub https://github.com/google/guava/issues/2071.

Read more comments on GitHub >

github_iconTop Results From Across the Web

initializing a Guava ImmutableMap - java - Stack Overflow
This is by design; the ImmutableMap class provides six different of() methods, accepting between zero and five key-value pairings. There is not an...
Read more >
Immutable Map Implementations in Java - Baeldung
Finally, we can use ImmutableMap.of() method to create an immutable map with a set of entries provided on the fly. It supports at...
Read more >
Best way to make a really big ImmutableMap - Google Groups
I've got a class that works fine, but its too big to make the javadoc compiler happy. It runs out o memory, thinking...
Read more >
ImmutableMap (Guava: Google Core Libraries for Java 16.0 API)
Returns an immutable map containing a single entry. This map behaves and performs comparably to Collections.singletonMap(K, V) but will not accept a null ......
Read more >
Immutable Map in Java (using Guava and Java 9)
This post will discuss various methods to create an immutable map in Java. ... like they are thread-safe, more memory-efficient, and can be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found