Saturday, 19 March 2016

Java 8 essentials summary Part I

Functional Interface


Functional Interface may be annotated by @FunctionalInterface. Functional interface has only one abstract method. This implies that functional interface exhibit single functionality. 

This method should not be confuse with default or static method in the interface which have implementation. Also any abstract method overriding one of the public method of java.lang.Object does not count. 

Default Methods in interface

 

Essentially Interface is 100% abstract. It is the responsibility of the classes to provide implementation for all the methods declared in the interface. 

Now we have a scenario where we want to add additional functionality to interface by declaring a new method in it. What will happen to the classes that have implemented the interface? 

Wow!!! All classes will break.

Here’s come Default method in the interface for our rescue. Default method is a method which has default in its signature and it has a concrete implementation. So the classes implementing the interface does not have to mandatory implement it. This helps us extending the interface functionality without breaking all the classes that implement it. There can be only one default method for a functional interface.

Static method in Interface

 

Java 8 provide static method for interface and we provide implementation for the static methods.

Below figure illustrate a simple Functional Interface and some related concept





Lambda

 

Lambda provide a very simple implementation of abstract method of the functional interface. Generic Lambda expression will look like

  (Type arg1.....) -> {   Body   }

Easiest way to visualize the implementation of lambda expression is the implementation of anonymous class as shown in the below figure.

We replace the first part marked in red with (Type arg1..) -> and remove the closing bracket as shown below. That’s All.

Now the question is why we do not need to specify the name of the interface method that will be implemented. This is because functional interface has only one abstract method  that needs to be implemented so it is just obvious what we are going to implemented. JVM has become smart enough to make this inference, also it can determine the type of the parameter required by method so that also we can omit. 

So to summarize we just remove the obvious with Lambda Expression.





There are number of one method interface or functional interface provided by java that can be extensively use with lambda expression.

Below is the illustration for java.lang.Runnable Interface which has run method as how the use of these method become straight forward with Lambda expression.


public class LambdaForOneMethodImpl {

    public static void main(String[] args) {
       
        Runnable r1=new Runnable(){

            @Override
            public void run() {
                System.out.println("You are running run anonymously" );
            }
           
        };

        r1.run();   
       
        Runnable r2=()->{System.out.println("You are running run with Lambda ");};
        r2.run();
    }
  
  
 }




One method interface are java.awt.event.ActionListener, java.util.Comparator, java.util.concurrent.Callable.

Java 8 has defined a lot of functional interfaces in java.util.function package and these can be extensively used with lambda expression




Additional resource
https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
http://www.tutorialspoint.com/java8/java8_lambda_expressions.htm
http://tutorials.jenkov.com/java/lambda-expressions.html



No comments:

Post a Comment

Streaming with Kafka API

The Kafka Streams API is a Java library for building real-time applications and microservices that efficiently process and analyze large-sca...