Java 8 | Lambda Expression

The lambda expression feature reshaped the Java language. Lambda expression comes with the functional programming construct to the object oriented programming. Lambda is nothing but an anonymous method.

Features:

  • Lambda expression reduces the number of lines of code.
  • Lambda expression provides implementation to the functional interface.
  • New capabilities are added to the API library.
  • Parallel processing of multi-core environments, especially handling of the for-each style of operations.

 Example:  


@FunctionalInterface

package in.co.bitbyte.java8;

public interface Developer {

public void writeReadableCode();

}

 Implementation of Functional Interface(without lambda expression):


package in.co.bitbyte.java8;

public class Test {

public static void main(String[] args) {

Developer d = new Developer() {

@Override

public void writeReadableCode() {

System.out.println(“At bitbyte, developers  think simple”);

}

};

d.writeReadableCode();

}

}

Implementation of Functional Interface(with lambda expression):


package in.co.bitbyte.java8;

public class Test {

public static void main(String[] args) {

Developer d = () -> {

System.out.println("At bitbyte developers think simple");

};

d.writeReadableCode();

}

}

 This feature changing the way that code is written. Two primary reasons to change the style of coding:

Added new syntax elements that expressive power of the language.

In addition to this added new capabilities to the API library, a big step into the collection API for iteration.

Syntax:

Lambda operator:         (parameters)  -> {actions}

The left side of lambda operator indicates “parameters” right side of the lambda operator lambda body specifies the “actions” need to be performed by the lambda expression.

Lambda expressions are not executed on its own. It works with the implementation of the functional interface.

Before lambda expression, we used the anonymous inner class to implement a functional interface (Single Abstract Method). So many built-in interfaces in Java 7 Runnable, Comparable and so on… Java 8 called this interfaces as functional interface some of the added functional interfaces are BiConsumer, Bi

Example:


package in.co.bitbyte.java8;

public class TestingBuiltInFunctionalInterface {

public static void main(String[] args) {

Runnable r = new Runnable() {

@Override

public void run() {

System.out.println("I am from run method of Runnable interface using anonymous inner class");

}

};

r.run();

}

}

We can implement the above same Runnable interface by using the lambda expression in a very simple way.

Example:


package in.co.bitbyte.java8;

public class TestingBuiltInFunctionalInterface {

public static void main(String[] args) {

Runnable r = () -> {

System.out.println("I am run method from Runnable interface using lambda expression");

};

r.run();

}

}

In the above example, we didn’t mention the method name of the interface because in the functional interface having exactly only one abstract method. We implemented the functional interface using the lambda expression.

If we want to do iteration in the collection, we will do iteration one of the following way.


package in.co.bitbyte.java8;

import java.util.ArrayList;

import java.util.List;

public class Ecommerce {

public static void main(String[] args) {

List list = new ArrayList();

list.add("Hybris");

list.add("ATG");

list.add("DemandWare");

list.add("Intershop");

for(String ecommerceTech : list)

{

System.out.println(ecommerceTech);

}

}

}

But the lambda expression defines a new style for the for-each operation. The main advantage here is we can achieve parallel processing. Without lambda, expression iteration is like processing each element.

Example:


package in.co.bitbyte.java8;

import java.util.ArrayList;

import java.util.List;

public class Ecommerce {

public static void main(String[] args) {

List list = new ArrayList();

list.add("Hybris");

list.add("ATG");

list.add("DemandWare");

list.add("Intershop");

list.forEach(ecommerceTech ->{System.out.println(ecommerceTech);});

}

}

What do you think?

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