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.
 

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You'll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You'll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


3 + seven =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.