Java 8 includes a new programming construct for anonymous functions called Lambda expressions. The basic idea behind these constructs are not new, and have in fact been around since LISP in 1958. But they are new to Java, and allow programmers to get started with new techniques of functional programming without having to learn a new language, said Maurice Naftalin, principal developer at Morningside Light Ltd., a technology transfer firm in Scotland.
Trisha Gee, Java engineer at MongoDB, expects the new style of programming enabled by Lambda expressions to be one of the carrots that encourages more organizations to quickly move to Java 8.
Database vendors like Oracle are also starting to implement direct support for Lambda and Streams, another programming construct being introduced into Java 8 applications, said Brian Goetz, Java language architect at Oracle.
For example, the Oracle Coherence distributed caching platform allows developers to use Lambda to create simple code constructs for filtering distributed queries across the cluster, as they would with an in-memory connection. Anything returning a stream of objects should be able to expose the same things, Goetz said.
Lambda on older Javas?
At the JavaOne 2014 conference, some developers expressed an interest in leveraging the Lambda programming capabilities with older versions of Java. MongoDB's Gee said this would be nice since it would allow developers to use newer features of Java without having to upgrade their development and deployment environments. But this would require making the library for implementing Lambda much larger, as the current implementation is able to leverage capability built into Java 8, rather than external libraries.
David Blevins, founder of Tomitribe, said that while this might be great for the short haul, in the long run it is only a stop gap measure, since using retro translators might lead to problems down the road.
Parallel streams coming
Conference participants asked about the feasibility for distributing application logic across parallel streams in general. Blevins noted that it seems natural to want to distribute stream queries, and there are a couple of efforts for doing this. The Oracle coherence approach is nice because it can use standard stream interfaces.
The introduction of the new stream method
Part of making collections Lambda-ready is introducing the default method concept in order to support the introduction of the new stream method to all collections, but so far there have been no complaints of that new construct being misused or abused. The fact is, in a modern, multi-core environment, supporting parallelism is important. When your programs can take advantage of parallelism, they can use hardware resources more efficiently. Cameron McKenzie
Another approach by Garett Swart is called a DistributableStream, which up-levels the concept of streams to a distributed environment. Blevins said this demonstrates how it is possible to build distributed queries that use different strands for different parts of the query. This is an experiment. Blevins said the concepts apply well to distributed calculations.
Test parallel streams code
One of the new features of Java 8 is support for running things in parallel, which may get overused to detriment. Developers were curious about when to use parallel and when not to.
Gee said the classic answer is to test new code with real examples in production, otherwise it will be difficult to determine whether parallel processing makes sense.
Morningstar's Naftalin said he is certain parallel stream processing will be overused. One of the things he learned while writing Mastering Lambdas: Java Programming in a Multicore World, was that some of the trial programs had some unexpected performance surprises. There is much to learn about the performance of parallel streams that needs to be taken into account.
While there is some guidance documentation now, it will take some time for people to learn best practices. Parallel streams only address a certain niche of problems and it's not easy to tell what they are up front, Naftalin said.
Blevins added that it is important to consider implementing performance metrics over time as the underlying libraries evolve. The Lambda team did not spend a lot of time tuning the underlying code as they made an architectural decision to get the benefits of the underlying fork join library and have not spent a lot of time optimizing the parallel streams components yet.
At the same time, Blevins expects parallel streams will get overused in the beginning as people attempt to solve problems with them.
Gee added, the problem with parallel streams is that it is too easy to implement it in and people think it will provide magic improvements to speed. She advocated that developers run tests with them turned on to see if the applications are getting benefits.
Be cautious with Lambda
Experts said Lambda is not appropriate for all applications. Morningside's Naftalin said that writing large functions reduces the clarity of the code. Lambda functions work best when they are kept to a single line of code. A good start is to implement cases like sorting.
Blevins said that over time, people will use Lambda for everything, and will discover it is more than required in many cases. In the end, it's about writing code that is easy to read. It will take a while to find the balance, which is the same thing that happens anytime a new feature is added. The same thing happened in 1998 between inter-classes and named classes. It is important to learn both approaches and then figure out which is more readable.
Blevins said everyone has to go at their own pace, do things wrong, and then essentially the best practices will become obvious in hindsight.
At the end of the day, the new support for Lambda expressions promises to open up new functional programming possibilities for developers without having to learn a new language, Naftalin said. "You should appreciate this opportunity. Normally you would expect to have to relearn everything. Now you can take small steps in an environment you are comfortable in."
Java 8 uses Lambda expressions for parallelism
Java 8 re-engineered with Lambda expressions