Lambda expression in Java - A new way of coding

How it looks if we have functionality of passing the method implementation as a parameter?Awesome isn't it? Yeah now we can do this magic in Java through Lambda expression.Oracle recently included Lambda expression in Java as a part of Java 8 release.

So let's explore few things about Lambda expression in Java.


A lambda expression is like syntactic sugar for an anonymous class with one method whose type is inferred. 

Syntax :
 The main syntax of a lambda expression is “parameters -> body”. The compiler can usually use the  context of the lambda expression to determine the functional interface being used and the types of  the parameters. There are four important rules to the syntax:
  • Declaring the types of the parameters is optional.
  • Using parentheses around the parameter is optional if you have only one parameter.
  • Using curly braces is optional (unless you need multiple statements).
  • The “return” keyword is optional if you have a single expression that returns a value.
Few examples of lambda expressions are :
 ()-> System.out.println("hello");
 (String name) -> System.out.println("hello"+name);
 (String fname, String lname) -> System.out.println("Hello "+fname+" "+lname);
 (int a,int b) -> { return a+b};




Following example shows usage of Lambda expression:

Create a Functional Interface (Click here to know more about functional interface )

@FunctionalInterface
 public interface BasicCalculation
{
   public int add(int a,int b);
 }


public class Calculate
{
BasicCalculation basic;

  public Calculate(BasicCalculation object)
      {
// TODO Auto-generated constructor stub
basic = object;
      }
 
     public int doAddition(int a,int b)
    {
  return basic.add(a,b);
    }
}



//Main class
public class TestClass2
{
public static void main(String[] args)
        {
int Sum = new Calculate((int a,int b)->{return a+b;}).doAddition(5, 6);
System.out.println(Sum);
}
}

Basically usage of Lambda expression looks like passing the implementation as an argument to the some other function which makes readability of the code even better and reduces the complexity of creating anonymous inner class.
Her comes one more example:

String[] players = { "Rafael Nadal", "Novak Djokovic", "Stanislas Wawrinka", "David Ferrer", "Roger Federer","Andy Murray", "Tomas Berdych", "Juan Martin Del Potro", "Richard Gasquet", "John Isner" };

Arrays.sort(players, new Comparator<String>()
 {
// Sort players by name using anonymous innerclass
   Arrays.sort(players, new Comparator<String>()
    {
         @Override
        public int compare(String s1, String s2)
         {
               return (s1.compareTo(s2));
        }
      }
  });

With lambda expression above code can be written as :
Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2)));

Similarly we can have Lambda expressions in various places for more info follow Oracle documentation about lambda expression.

To know more about Java 8 release follow below link :
https://leanpub.com/whatsnewinjava8/read

Happy Coding :)

Comments

Popular posts from this blog

Introduction to Deep Learning and Artificial Neural network.

Machine Learning - Work Flow

Functional Programming in JAVA