Java 8 is about a year away and comes with a language feature I really look forward to: Lambda Expression.
Sadly the other big feature, Modules for the Java Platform, has been delayed to Java 9. But nevertheless, bringing lambda expressions (or closures if you like) into the language will make programming in Java much better.
So nearly one year to go - but as Java is developed open source now, we can have a look and try to use it right now. So let’s go!
Download and Install Lambda enabled Java 8
First, I expected that I have to compile Java 8 myself, as it has not yet been release. But I was pleasantly surprised to see, that there are binary build available for all platforms at http://jdk8.java.net/lambda/. So I just downloaded the latest developer preview build and installed it on my computer.
To make sure it works, I created an LambdaIntro class containing a “Hello, World!”, compiled and executed it:
1 2 3 4 5
Note: I use the command-line to compile and execute here, because IDEs do not support Java 8 as of now.
The Non-Lambda Way
As an example lets assume I want to loop through a list of objects. But for my business logic, I need to have the value and the index of the list item. If I want to do it with current Java, I have to handle the index together with the actual logic:
1 2 3 4
This will output
1 2 3
This is not too bad, but I did two things in the same few lines of code: controlling the iteration and providing some (very simple) business logic. Lambda expressions can help me to separate those two.
The eachWithIndex method signature
So I want to have a method eachWithIndex which can be called like this:
1 2 3 4 5 6 7
The method receives two parameter. The first one is the list and the second one is a lambda expression or closure which instructs the method what to do with each list item. As you can see in line 3, the lambda expression receives two argument: the current value und the current index. These arguments do not have a type declaration. The type information will be inferred by the Java 8 compiler. After the arguments, there is a -> and a block of code which should be executed for every list item.
Note: You will have to write this method in a normal text editor or ignore the error messages inside your IDE.
Implement the eachWithIndex method
To use a lambda in Java 8, you need to declare a functional interface. A functional interface is an interface which has exactly one method - the method which will be implemented by the lambda expression. In this case, I need to declare a method which receives an item and an index and returns nothing. So I define the following interface:
1 2 3
With this interface I can now implement the eachWithIndex method.
1 2 3 4 5
The method makes use of the generic parameter
The nice thing about using functional interfaces is, that there are a lot of them already out there in Java. Think for example of the java.util.concurrent.Callable interface. It can be used as a lambda without having to change the code which consumes the Callable. This makes a lot of the JDK and frameworks lambda enabled by default.
Using a method reference
One little handy thing coming from Project Lambda are method references. They are a way, to re-use existing methods and package them into a functional interface object. So let’s say I have the following method
1 2 3 4
and I want to use this method in my eachWithIndex method, than I can use the :: notation inside my method call:
Looks nice and concise, doesn’t it?
This makes my first lambda example run. I couldn’t avoid a smile on my face to see closures running in one of my Java program after longing for them so long. Lambda Expression are currently only available as a developer preview build. If you want to find out more, read the current Early Draft Review or go to the Project Lambda project page.
I uploaded the full example code to gist.