Lambdas and Functional Interfaces

This is a one big contribution of java 8 in development. Lambdas treat functionality as method argument.

In its simplest form, a lambda could be represented as a comma-separated list of parameters, the! symbol and the body. For example:

Arrays.asList( “a”, “b”, “d” ).forEach( e -> System.out.println( e ) );

argument e  is being inferred by the compiler.

Here is some example of code:

List<String> dataIn=Arrays.asList("Sameer","Weds","Pooja");

/**
* Printing of all elements in dataIn to standard output
*/

dataIn.forEach(e->System.out.print(e+" "));
List<Integer> dataInts=Arrays.asList(4,3,7);

/**
* Sorting of all data using lambda expression
*/

dataInts.sort((o1,o2)->{;
    if(o1<o2){
       return -1;
     }else if(o1>o2){
       return 1;
      }
 return 0;
});

System.out.println();
dataInts.forEach(e->System.out.print(e+" "));
System.out.println();

If we have our own Interface and we want to use by lambda expressions. Here is an example, Lets first create a Sample Interface like as below:

public interface SampleInterface {

int sum(int a, int b);
}

As SampleInterface have only one method so we can use it in lambda expression as.

/**
* Created a Sample Interface to show usage of lambda expression using that also
 */

SampleInterface sum = (a,b) -> a+b;

System.out.println("Sum = "+sum.sum(1,2));

Output:

Sum = 3

As lambda expression is limited to only one method interface so how we handle if we have more than one method in a interface. Java8 provided a solution to this problem by including static and default methods in interfaes. Please find a basic example of one more method in Sample Interface

/**
 * Default methods make interfaces somewhat similar to traits but serve a bit different goal.
 * They allow adding new methods to existing interfaces without breaking the binary compatibility
 * with the code written for older versions of those interfaces.The difference between default methods
 * and abstract methods is that abstract methods are required to be implemented. But default methods are not.
 * Instead, each interface must provide so called default implementation and all the implementers will inherit
 * it by default (with a possibility to override this default implementation if needed). Let us take a look on example below.
 */

default void print(Object o){
   System.out.println(o.toString());
}

If this method is not marked as default a compilation error will be thrown.

Thanks

 

Advertisements

One thought on “Lambdas and Functional Interfaces

Add yours

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

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: