Round 2 feedback
Michael Nascimento
misterm at gmail.com
Wed Feb 13 07:19:44 PST 2013
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:
>>
>> numbers.optionalGet("six").orElse(0);
>>
>> 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 ->
totalByCity.optionalGet(city).orElse(0L));
This is the simplest case, I have others in which I need to create a
nested singleton map, for instance. This is a fairly common scenario
in this code base, which is heavily collection-based.
Hope this helps.
Regards,
Michael
More information about the lambda-dev
mailing list