Streams in Java 8

Streams in Java 8

Since Java 8 the JDK has a Stream API. Among numerous other operations the Stream API offers two operations – reduce() and collect() – which are occasionally misunderstood and thus used incorrectly – at the expense of introducing subtle bugs in conjunction with parallel streams.

This tutorial discusses the background of this common misconception: reduce () is an operation that stems from functional programming languages, where objects are typically immutable. Java, in contrast, is an object oriented language, where most objects are mutable. Hence, Java needs something like a “mutable reduction”. This is what the stream operation collect() offers. As both operations – reduce() and collect() – are very similar, they are occasionally mixed up, which can lead to concurrency issues when executed on parallel stream. This tutorial explains why.

Video producer: http://www.jdays.se/