DevoxxUK Lambdas Lab

Stephen Colebourne scolebourne at joda.org
Wed Apr 3 05:50:59 PDT 2013


On 3 April 2013 13:31, Brian Goetz <brian.goetz at oracle.com> wrote:
>> ACTION:
>> Add a static method to Map.Entry:
>> public static <K, V> Entry<K, V> of(K key, V value) {
>>    return new AbstractMap.SimpleImmutableEntry<K, V>(key, value);
>> }
>
> Been down this rathole, and we retreated.  While you can make a stream out
> of Map.Entry, doing anything more than simple filtering and forEaching is
> unlikely to result in a happy outcome.  If it hurts, don't do it.

The static method above is useful outside of stream-based development.
SimpleImmutableEntry is a very hidden class that I didn't know about
until I requested that the functionality was added. However
SimpleImmutableEntry is also poor as it requires the generic
parameters as well, whereas a static method can set them for you.

To me, this is a static method I've had to write on a number of
occasions, and its really a no-brainer once you separate this request
from its possible use in streams. Its a classic example of point
lambdaification.


>> ACTION:
>> Add a much better way to convert a stream of Entry to a Map. Perhaps:
>>   .collect(Collectors.toMap(entry -> getKey(), entry ->
>> entry.getValue()));
>
> Or better yet, don't encourage people to make streams of Map.Entry.

I think thats hugely unrealistic. Maps are incredibly widely used in
Java development, perhaps even more than collections. To say that maps
can't be used with streams is a huge hole.

I really don't think the method above is a big ask, and it is
generally useful for other cases. For example, processing a
Stream<Person> with the goal of outputting Map<PersonId, Address>.


>> ACTION:
>> Can there be some kind of reducingSum() method? It feels like a common
>> use case, and might provide the source code for people to look at
>> providing the example code for them to then learn and write more
>> advanced reducers themselves.
>
> Do you mean something like:
>
>   <T> Collector<T, Integer> counting() {
>     return reducing((T t) -> 1, Integer::sum);
>   }

Yes. Although I named it reducingSum() partly so users could look at
the source code and start to learn how reducing works by example.

Stephen


More information about the lambda-dev mailing list