Java Reference
In-Depth Information
List<Apple> heavyApples =
inventory .stream() .filter((Apple a) -> a.getWeight() > 150)
.collect(toList());
Parallel processing:
import static java.util.stream.Collectors.toList;
List<Apple> heavyApples =
inventory .parallelStream() .filter((Apple a) -> a.getWeight() > 150)
.collect(toList());
Chapter 7 explores parallel data processing in Java 8 and its performance in more detail. One of
the practical issues the Java 8 developers found in evolving Java with all these new goodies was
that of evolving existing interfaces. For example, the method Collections.sort really belongs to
the List interface but was never included. Ideally, you'd like to do list.sort(comparator) instead
of Collections.sort(list, comparator). This may seem trivial but, prior to Java 8, you can update
an interface only if you update all the classes that implement it—a logistic nightmare! This issue
is resolved in Java 8 by default methods .
Parallelism in Java and no shared mutable state
People have always said parallelism in Java is difficult, and all this stuff about synchronized is
error prone. Where's the magic bullet in Java 8? There are actually two magic bullets. First, the
library handles partitioning—breaking down a big stream into several smaller streams to be
processed in parallel for you. Second, this parallelism almost for free from streams works only if
the methods passed to library methods like filter don't interact, for example, by having mutable
shared objects. But it turns out that this restriction feels quite natural as a coder (see, by way of
example, our Apple::isGreenApple example). Indeed, although the primary meaning of
functional in functional programming means “using functions as first class values,” it often has
a secondary nuance of “no interaction during execution between components.”
1.4. Default methods
Default methods are added to Java 8 largely to support library designers by enabling them to
write more evolvable interfaces. We cover them in detail in chapter 9 . They're important
because you'll increasingly encounter them in interfaces, but because relatively few
Search WWH ::




Custom Search