ImmutableMap::of should accept more entries
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.
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 [email protected] 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.
I guess that's personal taste, but basically for each map it adds one line of code (.build()), and per entry five additional characters. And from a visual point of view your brain has to ignore more boilerplate while reading code. So personally I find using ImmutableMap.of a lot nicer.
When you push an api to the point where it feels awkward, then you may be using the wrong tool for the job. For json specifically, there are libraries like jsonschema2pojo that code generate the boilerplate and then marshall with a json serializer.
I feel that any of such pojo generator libraries are an overhead for my use case and would slow me down considerably while prototyping. I need to quickly iterate on the json schema and add/remove attributes at any place while I go along. For that, the ImmutableMap.of is just perfect. I agree that for production-use or exposing such json objects to end-users as type-safe classes it makes sense to use jsonschema2pojo or similar. It's just two different use cases.
It may help us to generate a histogram of how many entries people are putting in their ImmutableMap.builder().....build() statements. Maybe there will be clear evidence.
refaster-matcher-hits-Entry6 N refaster-matcher-hits-Entry7 0.7 N refaster-matcher-hits-Entry8 0.63 N refaster-matcher-hits-Entry9 0.39 N refaster-matcher-hits-Entry10 0.32 N
Kurt, can we get that compared for entries <= 5 so we know how sharp the dropoff is after that point?
On Thu, Jul 30, 2015 at 12:42 PM Kurt Alfred Kluever < [email protected]> wrote:
refaster-matcher-hits-Entry6 N refaster-matcher-hits-Entry7 0.7 N refaster-matcher-hits-Entry8 0.63 N refaster-matcher-hits-Entry9 0.39 N refaster-matcher-hits-Entry10 0.32 N
— Reply to this email directly or view it on GitHub https://github.com/google/guava/issues/2071#issuecomment-126451572.
Yep, I'm running that right now...sorry, I should have included that last time.
For those you'll have to sum up the builder-style and the of() calls, yes?
On Thu, Jul 30, 2015 at 12:47 PM, Kurt Alfred Kluever < [email protected]> wrote:
Yep, I'm running that right now...sorry, I should have included that last time.
— Reply to this email directly or view it on GitHub https://github.com/google/guava/issues/2071#issuecomment-126452848.
Kevin Bourrillion | Java Librarian | Google, Inc. | [email protected]
Refastor mentions hurt my heart :broken_heart:! Open source as a Christmas present :grinning:
@kevinb9n Yea, I'm actually separating them out during the Refaster run (I'm curious how many people use the builder when they could use using .of()), but I'll sum them manually.
@JakeWharton It's a P1 goal this quarter for @lowasser - fingers crossed :-)
1 N 2 53% N 3 28% N 4 15% N 5 8.5% N 6 5% N 7 3.6% N 8 3.4% N 9 2% N 10 1.7% N
More or less follows N/2 for each extra pair of K/V.
Okay, so there's no great justice behind the choice of 5.
I note that ImmutableList and others go to 11 even though the advantage in going further is much less (a varargs form takes over after that).
On Thu, Jul 30, 2015 at 1:17 PM, Kurt Alfred Kluever < [email protected]> wrote:
1 N 2 53% N 3 28% N 4 15% N 5 8.5% N 6 5% N 7 3.6% N 8 3.4% N 9 2% N 10 1.7% N
More or less follows N/2 for each extra pair of K/V.
— Reply to this email directly or view it on GitHub https://github.com/google/guava/issues/2071#issuecomment-126468933.
Kevin Bourrillion | Java Librarian | Google, Inc. | [email protected]
So do we want to follow that precedent and turn this up to 11?
So do we want to follow that precedent and turn this up to 11?
Are there regression benchmarks to see how this would affect Android?
I'm not sure what benchmarks you have in mind?
I'd been thinking of class loading being impacted by the new methods. But now that I think of it, Proguard should take care of removing the unused methods.
@ustaDAH, can you give us some more context? Like a sample of your builder-using code that doesn't seem to work?
This is an example

It requires List
@konradr25, it's not clear to me what you're looking for there. That case only uses one map entry, so it can use ImmutableMap.of, and I'm not sure how you expect us to improve on that case. This issue is for expanding ImmutableMap.of to accept more than 5 entries.