JDK8 Lambdas @ #JavaOneBR

Simon Ritter (@speakjava) presentation was about the JDK8 Lambdas and Streams on JavaOne Latin America 2015.

You may think that you’ve read and know all about it, but Simon was able to deliver a really different point of view, JDK8 Lambdas and Streams are here to take out the complexity of parallel programming from the developer point of view.

As platform, JDK supported concurrency since it’s creation in 1995 with Threads. Times fly-by and in
JDK 5 JSR166 was there and so the java.util.concurrent and over the next iterations, we’ve seen more and more being added to the JDK and was time to stop it. More doesn’t translate to better and as we can see in JDK7, we have a plethora of options to make our code run in parallel, but we still control in serial this basic type of execution flow:

List students = ...  
double highestScore = 0.0;  
for (Student s : students) {  
   if (s.getGradYear() == 2011) { 
      if (s.getScore() > highestScore) highestScore = s.score; 

Inherently serial, the task of iterate from beginning to end is handled over to the developer, the business logic is entangled with the iteration code and the code generated is not thread-safe due to the mutable accumulator variable.

In the JDK7 inner classes iteration was available, but still a no-go due to ugliness and code low level readability due to the noise.

In JDK8 lambdas comes to the rescue. The same code above is easily rewritten as:

List students = ...  
double highestScore = students.stream()  
                                .filter(s -> s.getGradYear() == 2011)
                                .map(s -> s.getScode())

The iteration now is handled by the JDK itself instead of the developer. Much less noise, much more concise and the code is now thread-safe without synchronization primitives.

So what are Lambdas expressions?

  • Lambdas expressions are anonymous inner functions (and not methods)
  • Isn’t associated with a class
    • So no this, this in this case refer to the enclosing object of the lambda.
  • Can be used whenever you’d use a anonymous inner class
  • Syntax is ( [optional parameters] ) -> body, the rules are:
    • No parameters? use ()
    • One parameter? the usage of the parenthesis is optional
    • Two or more parameters? Enclose the parameters in parenthesis (x,y)

Lambdas are Functional Interfaces, which are Single Abstract Method Type or just SAM. As from JDK8, Interfaces are allowed to have static methods so isn’t quite easy to identify SAM’s for their single and lonely abstract method.

There’s one more neat thing about Lambdas, the Method Reference in which whenever you find yourself writing this:

String words = filecontents.stream().filter(s -> s.length).collect(Collectors.joining(","));  

Do this instead with the help of the Method Reference operator:

String words = filecontents.stream().filter(String::length).collect(Collectors.joining(","));  

You may find more about Lambda Expressions here in this page and Method Reference over here

and here is the same presentation shown at JavaOneBR:

Leave A Comment

Your email address will not be published. Required fields are marked *