Streams in Java 8: Reduce vs. Collect

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 strams. In this tutorial we will discuss 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 parallelstream. The tutorial will explain why. jDays 2016