MapStream.map* methods
Brian Goetz
brian.goetz at oracle.com
Tue May 8 08:17:56 PDT 2012
On May 8, 2012, at 1:06 AM, Peter Levart wrote:
> Hi Mike and others,
>
> I'm trying to understand the purpose of the 3 MapStream.map* methods that have
> the following signatures:
>
> public interface MapStream<K, V> {
>
> <W> MapStream<K, W> map(BiMapper<K, V, W> mapper) default ...
>
> <W> MapStream<K, W> mapValues(Mapper<V, W> mapper) default {
> return map( (k, v) -> mapper.map(v));
> }
>
> <W> MapStream<K, Iterable<W>> mapValuesMulti(
> BiMapper<? super K, ? super V, Iterable<W>> mapper
> ) default ...
>
> ... the first is obvious. It replaces the values in a key/value pairs with
> values computed by BiMapper from key/value pairs. This is the most common
> method and for the less common case (to replace keys instead of values) one
> can use the following idiom:
>
> mapStream.swap().map().swap()...
>
> ... the second (mapValues) is just a shorthand for the first in the common case
> when keys are not needed in the computation of values.
The reason for this shorthand version is that you may have in hand a method that does the mapping already:
people.mapped(Person::getBirthday) // stream of (Person, Date)
.mapValues(Date::toString) // stream of (Person, String)
instead of
...mapValues( (person, birthday) -> birthday.toString())
> On the other front, it would be nice to have the following method in
> MapStream:
>
> MapStream<K, Iterable<V>> groupByKey();
Good catch.
More information about the lambda-dev
mailing list