Uses and limitations of the Stream API in Java 8

The Stream API in JDK 8 promises to bring a lot of expressive power to the Java language. However, the API and the framework do have some limitations. Compared to frameworks in other languages, some key features are missing, including stateless lambda expressions.

Software Engineering blogger David Hartveld rounds out his series of three posts focusing on Java 8 with one introducing the Stream API, demonstrating its extensive use of lambda expressions. Here, he compares a pre-Java 8 for-loop:

List<Block> blocks = /* ... */;
int sumOfWeights = 0;
for (Block block : blocks) {
  if (block.getColor() == Color.RED) {
    sumOfWeights += block.getWeight();

to one expressed with the JDK 8 Stream API:

List<Block> blocks = /* ... */;
int sumOfWeights =
                      .filter(b -> b.getColor() == Color.RED)
                      .map(b -> b.getWeight())

Visit David's Software Engineering blog for a quick introduction to the moving parts in the improved for-loop, then find out why he believes that leaving stateless lambda expressions out of the new Stream API is a mistake.

This story, "Uses and limitations of the Stream API in Java 8" was originally published by Java Everywhere.