Lambda expressions: Any thoughts?

JDK8.0 has introduced Lambda expression. According to official docs from Oracle “Lambda Expressions, a new language feature, has been introduced in this release. They enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (referred to as functional interfaces) more compactly.

While developing a client socket app, I first viewed the following code in one of the tutorials online. (URL: http://www.hascode.com/2014/11/creating-different-websocket-chat-clients-in-java/)

clientEndPoint.addMessageHandler(responseString -> {
System.out.println(jsonMessageToString(responseString, roomName));
});

My initial thought was “Okay. So they put it on the next line since there is not enough space“. That’s how the online sample codes are usually shown in many tutorials/blogs/publications, when a continuation of a block of code does not fit it one line, the newline is indicated with the use of arrow symbol like this ->. And that’s it!!!

Surprise! Surprise! Surprise! This symbol is the lambda notation.

Lets’ see a few examples:

Example 1:

Old style when using Runnable

Runnable r1 = new Runnable(){
@Override
public void run(){
System.out.println(“Hello world one!”);
}
};

JDK 8.0 Lambda Style Runnable

Runnable r2 = () -> System.out.println(“Hello world two!”);

Example 2:

Old style with Collections


Collections.sort(personList, new Comparator<Person>(){
public int compare(Person p1, Person p2){
return p1.getSurName().compareTo(p2.getSurName());
}
});

JDK 8.0 Lambda Style Collections

Collections.sort(personList, (Person p1, Person p2) -> p1.getSurName().compareTo(p2.getSurName()));

Example 3:

Old style with Listener

button.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent ae){
System.out.println(“Click Detected by Anon Class”);
}
});

JDK 8.0 Lambda Style Listener

testButton.addActionListener(e -> System.out.println(“Click Detected by Lambda Listner”));

Source URL:
http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#approach1

And a lot more can be found in the official doc from Oracle. These are just a few.

My thoughts:
If you consider the above examples, it is pretty clear that the code is concise and readable. Question is: is it really understandable, especially for developers like us who have been involved in the old style Java coding for a long time? Who knows? May be, we will get used to it. When generics, auto-boxing etc. were introduced, we all panicked at first. But now can we think without those features?
I mean, who does coding like this anymore

<<
for(int i=0; i<personsList.size(); ++i) {
Person p = (Person)personList.get(i);
}
>>

What about the above solution?
Well, I contacted with the developer at hasCode.com. After a bit of investigation, I was able to convert the above code to old JDK7.0 style which is as follows:


clientEndPoint.addMessageHandler(new MyMessageHandler() {
@Override
public void handleMessage(String responseString) {
System.out.println(jsonMessageToString(responseString, roomName));
}
});

(Old is gold! )

The reason? Well truly speaking, JDK 8.0 is still at its pre-matured state. And I am not ready to use it (at least, for the moment)

Posted in: JEE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s