Java Reference
In-Depth Information
you can think of them as fancy iterators over a collection of data. In addition, streams can be
processed in parallel transparently , without you having to write any multithreaded code! We
explain in detail in chapter 7 how streams and parallelization work. Here's a taste of the benefits
of using streams: compare the following code to return the names of dishes that are low in
calories, sorted by number of calories, first in Java 7 and then in Java 8 using streams. Don't
worry about the Java 8 code too much; we explain it in detail in the next sections!
Before (Java 7):
In this code you use a “garbage variable,” lowCaloricDishes. Its only purpose is to act as an
intermediate throwaway container. In Java 8, this implementation detail is pushed into the
library where it belongs.
After (Java 8):
To exploit a multicore architecture and execute this code in parallel, you need only change
stream() to parallelStream():
List<String> lowCaloricDishesName =
menu.parallelStream()
.filter(d -> d.getCalories() < 400)
 
Search WWH ::




Custom Search