News for hackers. Stuff that matters.
Re:Love It or Hate It, Java Continues to Evolve by Nursie on Monday June 17, 2019 @05:28AM (#20201235)
I've not yet worked on anything more recent than 8 but ...

I have a Map of Lists of Foo items (foos), and I want to transform each item in the sublists to a Bar, filter out bad Bars, transform to Baz and keep the results as a new list of Bazs

  List<Baz> barList = foos.values()
                          .stream()
                          .flatMap(List::stream)
                          .map(Foo::transformToBar)
                          .filter(Bar::isBad)
                          .map(Baz::transformToBaz)
                          .collect(Collectors.toList());

The syntax is a little clunky at times... but once you get used to it it's quite expressive.

There are no comments on this comment yet.