Lambda it2 - split?
Brian Goetz
brian.goetz at oracle.com
Thu Sep 13 13:41:29 PDT 2012
There are a few ways to do this, depending on the shapes of your data.
One way is:
people.stream().groupBy(Person::getCity)
.reduceValues(...);
-> yields a Map<City, U> where U is the reduction
Another is
people.stream().foldBy(Person::getCity,
() -> new Averager(),
(avg, e) -> avg.updateWith(e))
.mapValues(avg -> avg.compute())
.into(new HashMap<>());
-> yields a Map<City, U>
which is probably more efficient.
On 9/13/2012 2:01 AM, Janda Martin wrote:
> Hi,
>
> I have newbie question. Does lambda streams support some kind of split operation?
>
> Virtual problem:
>
> Input: collection of cities
> List<City> cityList
>
> Output: average age for all people
> double avgAge
>
> Steps to solve:
> 1) Each city MAP to collection of people
> 2) Compute AVG over collection of people
>
> I know that I can
> - MAP input value exactly to another value
> - REDUCE collection to one value
>
> But can I?
> - SPLIT input value into 'stream' (not collection) of another values
>
>
> Thank you very much for answer
>
> Martin
>
> PS Sorry for my bad English. I hope that you understand what I mean.
>
More information about the lambda-dev
mailing list