Closed GoogleCodeExporter closed 9 years ago
How does this not work?
Multimap namesByBirthYear = Multimaps.transformValues(
Multimaps.index(persons, personToBirthYearFunction),
personToNameFunction);
Original comment by ogregoire
on 18 Sep 2012 at 4:28
@ogregoire: that's a very nice approach!
That said, I'm not fully convinced that the OP's suggestion actually improves
readability over the direct approach without any helper methods:
ImmutableListMultimap.Builder<K, V> builder = ImmutableListMultimap.builder();
for (Person p : people) {
builder.put(p.getName(), p.getBirthYear());
}
return builder.build();
Original comment by wasserman.louis
on 18 Sep 2012 at 6:05
@wasserman.louis: this works as long as you have simple getXxx(). With more
complicated function, simple loop is not elegant any more.
@ogregoire: sure, this will work as well and looks like preferred Guava way, as
there is no transformEntries() for Map to change type of the key.
Original comment by kofem...@gmail.com
on 18 Sep 2012 at 7:51
@kofemann: If a Function can do what you want, you can always just write a
method that will keep it equally elegant.
builder.put(complicatedFunction(p), anotherFunction(p));
Original comment by cgdec...@gmail.com
on 18 Sep 2012 at 8:51
Yep. You can always factor that out into its own method -- and in most cases I
can think of where the logic is that complicated, you *should.*
Original comment by wasserman.louis
on 18 Sep 2012 at 8:57
Right, and here you see the difference between
ImmutableListMultimap.Builder<K, V> builder = ImmutableListMultimap.builder();
for (Person p : people) {
builder.put(complicatedFunction(p), anotherFunction(p));
}
return builder.build();
and
return Multimaps.index(persons, complicatedFunction, anotherFunction);
Anyway, you can clone the issue.
Original comment by kofem...@gmail.com
on 18 Sep 2012 at 9:06
Yes, that is the right side-by-side comparison, but I'm not sure option 2 is
*necessarily* better than option 1. "Multimaps.index" doesn't help me figure
out what that call is actually doing, whereas the builder approach makes it
very obvious exactly what you're doing there.
Original comment by wasserman.louis
on 18 Sep 2012 at 9:23
Original comment by lowas...@google.com
on 14 Aug 2013 at 10:09
We like the alternatives in comment #1 and #2 instead of adding a new API.
Original comment by kak@google.com
on 22 Aug 2013 at 10:21
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:13
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:08
Original issue reported on code.google.com by
kofem...@gmail.com
on 18 Sep 2012 at 1:55