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.
Its awesome
Lambda expression adds that missing link of functional programming to Java. Lambda expression let us have functions as first class citizen. Many thanks for sharing this.
Its actually a great and helpful piece of information. I am satisfied that you simply shared this helpful info with us.Looking forward to reading more from you. I finally decided to write a comment on your blog. I just wanted to say good job. I really enjoy reading your post and I collect some idea on java. Thank you
Java lambda expressions are Java’s first step into functional programming. Thanks for sharing this information.
Very Nice!!!!
thanks for the information