Articles about Java streams often repeat a bunch of tropes: streams are for succinctness, ugly mechanics are the norm, anemic pipelines, magic collectors, and weak exception handling. This is a rebuttal to all of them!
Java 8 brought lambda expressions, streams, and default methods to Java. More than that, it was the first fundamental addition to the language in ten years and showed that the platform is alive and kicking.
This not only changed some APIs and let everybody use streams instead of loops. The enormous simplification for passing behavior as data allowed Java 8 to embrace functional programing. It enabled the adoption of many FP techniques and deeply changed how Java developers write code – more precisely as the community is coming to grips with the new possibilities it is still changing our coding style.
If your team would benefit from an introductory or advanced course on Java 8, you can hire me as a trainer.
When using Stream.findFirst() or findAny(), you will often assume that there is at most one element left in the stream. But neither tests that assumption so maybe you should use a different approach.
After my last post you had some ideas about how else to benchmark stream performance. I implemented them and here are the results.
A close look at stream performance. How do they compare to for and for-each loops oder arrays and lists. And what roles play boxing and the complexity of the executed operation?
Stephen Colebourne presented his pragmatic approach to using Optional. I argue for a stricter one that gets us further without considerable downsides.
Proposal to implement new casting methods on Java’s Class. They aim to fulfill the need for improved ways to cast which was created by Java 8’s Optional and Stream.
Anonymous classes are verbose and obfuscating. Functional implementations can oust them from their last strongholds (“almost-functional” interfaces and abstract classes).
Why interface evolution with default methods does not work for whole interfaces – at least not smooth enough to be practical.
Java has no Elvis operator (or null coalescing operator / null-safe member selection) but with lambda expressions / method references you can roll your own.
Patterns for interface evolution with default methods: gradually add, replace and remove interface methods without breaking client code.