Simple Lambda – Java8


public class LambdaSample {
  protected interface SimpleCalc {
    int operation(int a, int b);
  }
  public static void main(String[] args) {
    /*
     * Old style
     */
    SimpleCalc add = new SimpleCalc() {
    @Override
    public int operation(int a, int b) {
      return a + b;
    }
  };
  System.out.println(add.operation(2, 5));
  /*
   * New style - Lambda Expression
   */
  SimpleCalc multiply = (a, b) -> a * b;
  System.out.println(multiply.operation(2, 5));
  }
}

In the above code you can see the old vs new style of anonymous function creation. Lambda expression makes it short, concise and readable. [its look readable to me :)]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s