There are some feelings in the functional programming community that Java 8 didn’t go far enough. However, this is not the case for Simon Ritter. In Streams in JDK 8: The Good, The Bad and The Ugly he looks at the API with Oracle’s Stuart Marks. We asked him about Streams API. Also, ahead of his 55 New Features in JDK 9 talk at Devoxx US, we asked him what we can look forward to in the Java 9 release.
What is the biggest quality of life improvement we can look forward to in Java 9?
To me, this is an easy one, but one that is reflected in a very small change that does not impact the language, libraries or JVM. The answer is that the Javadocs are now searchable! With over four thousand public classes in the JDK and at least 10x as many methods it’s often hard to find what you’re looking for; even scrolling down the class list is a pain. Finally, a search field!
What is the most interesting JVM improvement?
That’s a bit more tough. I think that JEP 254: Compact Strings is an interesting change. It’s something that seems like it should be pretty straightforward but anyone who’s listened to Alexsey Shipilev talk about this will know that it’s very involved underneath. From a personal perspective I also think JEP 285: Spin-Wait Hints is interesting, primarily because it’s the first JEP submitted from outside of Oracle (us at Azul) that’s been included in a release.
Can we expect any improvements to the Java 8 Streams API?
Yes, there are a few additional APIs being included in JDK 9.
dropWhile are like skip and limit, but using a
Predicate rather than a specific count.
Files.lines is also very useful, as it now allows lines read from a file to be processed efficiently through a parallel stream by memory mapping the file and dividing it based on line-breaks. There are a few more stream sources introduced in JDK 9 as well.
On the Streams API, do you think it goes far enough to introduce functional programming features to Java?
Yes, definitely. Java had always been an imperative language until the introduction of streams. Functional programming is very different to imperative and it takes some getting used to. It really requires you to think differently and this can be a challenge for Java programmers with no prior functional language experience (like me). Streams provides a powerful, yet approachable way to use a functional style of programming in Java. Maybe in the future Java will get more functional elements but I think for people looking for that at the moment, Scala is the way to go.
What is the most abused aspect of the Streams API?
forEach method. There’s absolutely nothing wrong with it and it’s very useful when used correctly. The problem is that, as Java programmers, we think in terms of loops to process collections of data. The temptation is to think, “Right, I need to do this for each element in my collection. Oh look, there’s a
forEach method, I’ll use that”. My advice is when you think you want to use
forEach stop and think again to see whether you should be using a filter-map-reduce approach.
What is the best aspect of the Streams API?
To me it’s the clarity it gives your code. At first streams looks quite foreign, especially when you have multiple steps all using Lambda expressions. Once you get used to the syntax and what’s going on, the higher level of abstraction and simplicity of approach can lead to some very elegant (and easy to maintain) code.
For more, see Simon’s talks at Devoxx US.