A simple application of Lambda Expressions in Java 8

I have been trying to fit in lambda expressions in the code I write and this simple example is a consequence of the same. For those totally unaware of Lambda Expressions in Java, I would recommend them to read this first before getting into this post.

Ok, now that you are familiar with Lambda Expressions (after reading the introductory post), lets go in the simple example which I thought of as a good use of lambda expression.

Consider this scenario: A certain operation is surrounded by some pre-processing and some post-processing. And the operation to be executed can vary depending on
 
the behaviour expected. The pre-processing code extracts the required parameters for the operation and the post-processing does the necessary cleanup.

Lets us see how this can be done with the use of Interfaces and their implementations via Anonymous Inner classes.

Using anonymous inner classes

An interface which has to be implemented to provide the required behavior:

interface OldPerformer {
  public void performTask(String id, int status);
}

And lets look at method which performs the pre-processing, executes the required operation and then the post-processing:

public class PrePostDemo {
  static void performTask(String id, OldPerformer performer) {
    System.out.println("Pre-Processing...");
    System.out.println("Fetching the status for id: " + id);
    int status = 3;//Some status value fetched
    performer.performTask(id, status);
    System.out.println("Post-processing...");
  }
}

We need to pass 2 things- an identifier to perform the pre-processing and an implementation of the operation, which can be done as shown below:

public class PrePostDemo {
  public static void main(String[] args) {
    //has to be declared final to be accessed within
    //the anonymous inner class.
    final String outsideOfImpl = "Common Value";
    performTask("1234", new OldPerformer() {
      @Override
      public void performTask(String id, int status) {
        System.out.println("Finding data based on id...");
        System.out.println(outsideOfImpl);
        System.out.println("Asserting that the status matches");
      }
    });
    performTask("4567", new OldPerformer() {
      @Override
      public void performTask(String id, int status) {
        System.out.println("Finding data based on id...");
        System.out.println(outsideOfImpl);
        System.out.println("Update status of the data found");
      }
    });
  }
}

As seen above, the variables declared outside of the Anonymous inner class have to be declared as final for them to be accessible in the methods of the anonymous inner class. The output of the above code would be:

Pre-Processing...
Fetching the status for id: 1234
Finding data based on id...
Common Value
Asserting that the status matches
Post-processing...
Pre-Processing...
Fetching the status for id: 4567
Finding data based on id...
Common Value
Update the status of the data found
Post-processing...

Using Lambda expression

Lets look at how the above can be written using the lambda expression:

public class PrePostLambdaDemo {
  public static void main(String[] args) {    
    //Need not be declared as final for use within a 
    //lambda expression, but has to be eventually final.
    String outsideOfImpl = "Common Value";

    doSomeProcessing("123", (String id, int status) -> {
      System.out.println("Finding some data based on"+id);
      System.out.println(outsideOfImpl);
      System.out.println("Assert that the status is "+status );
    });

    doSomeProcessing("456", (String id, int status) -> {
      System.out.print("Finding data based on id: "+id);
      System.out.println(outsideOfImpl);
      System.out.println("And updating the status: "+status);
    });
  }

  static void doSomeProcessing(String id, Performer performer ){
    System.out.println("Pre-Processing...");
    System.out.println("Finding status for given id: "+id);
    int status = 2;
    performer.performTask(id, status);
    System.out.println("Post-processing...");
  }
}

interface Performer{
public void performTask(String id, int status);
}

Apart from the interesting lambda expression syntax, the variable outside the scope of the lambda expression is not declared as final. But it has to be eventually final, which means that the value of the variable: outsideOfImpl shouldn’t be modified once declared.

This is just another cleaner way of using lambda expression in place of Anonymous inner classes.

A parting note: The schedule release of JDK 8 has been pushed further into Feb 2014 and the complete schedule can be found here. I am using the Project lambda build which keeps getting updated each day, so feel free to let me know if something of this doesn’t work on latest builds. I will try my best to keep updating the builds and trying out the samples posted here.

Another note: Dont get overwhelmed by whats happening in Java 8, these features are already part of lot of programming languages now. I found that learning the syntax and approach of lambda expressions in Java has helped me to understand and think functionally and more specifically appreciate Scala closures.
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "A simple application of Lambda Expressions in Java 8"

  1. Arijit Debnath says:

    Its awesome

Leave a Reply


− 3 = one



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close