Round 2 feedback
gregg at wonderly.org
Wed Feb 13 07:51:48 PST 2013
On 2/13/2013 9:19 AM, Michael Nascimento wrote:
> On Wed, Feb 13, 2013 at 12:59 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
>>> Sorry again, I meant joiningWith... I wrote this without having access
>>> to the actual code I've written...
>> So, the idea is you take a Stream<T> and a function T->U and you get a
>> Map<T,U>. If we had tuples, this would be stream.map(t -> (t, f(t))).
> Yes, I had understood how it works already, especially given the
> example provided by you in the SortedMap thread. The problem is the
> method name is completely unintuitive here. And yes, I wish I had some
> suggestions... :-(
>>> That's why I think
>>> having collectUnordered as a different method feels odd.
>> Different sense of ordered/unordered. More docs needed. But
>> collectUnordered is there for the cases that the library can't possibly
>> figure out automatically. Like you've provided an associative but not
>> commutative combining function. Or you just don't care about order. (The
>> reason its even exposed is there are some significant optimizations that are
>> possible if ordering is not a requirement.
> I understand it is there because of optimizations and that is
> precisely why I feel it is odd to allow one to call collect(toSet())
> when you, the library designer, could somehow change the design so
> only the most effective combination is allowed here.
>>> No, what I want is something like:
>>> You need something this concise to use maps in lambda expressions.
>> Can you post a more complete example?
> Sure. Imagine you have a Set<String> representing some cities in a
> business application. You call your business layer, that invokes a
> query and retuns a Map<String, Long> to you, based on a query that was
> run. You are going to use this map more than once; for some uses, it
> matters whether the city was found in the database or not, for others,
> you want non-found cities to contain zero. So to create the new Map
> you will do:
> Map<String, Long> totalByCityForEveryCity =
> cities.stream().collectUnordered(joiningWith(city ->
This is similar in effect to a "left outer join" in SQL. There are lots of good
reasons to be able to do this, so that data is regular, and algorithms are
plagued with missing data checks. Being able to provide the default value,
instead of "nul" is very nice.
More information about the lambda-dev