Sunday, September 20, 2015

Functional Interfaces (java.util.function) in Java 8



Functional Interfaces (java.util.function):

  • Functional interface is also called as Single Abstract Method interfaces.
  • A functional interface is an interface with a single abstract method
  • Note: A functional interface can redeclare the methods in the Object class.
  • The Java API has many one-method interfaces such as java.lang.Runnable, java.util.concurrent.Callable, java.util.Comparator, java.awt.event.ActionListener etc.
      • public interface Runnable { void run(); } 
      • public interface Callable<V> { V call() throws Exception; }
      • public interface ActionListener { void actionPerformed(ActionEvent e); } 
      • public interface Comparator<T> { int compare(T obj1, T obj2); boolean equals(Object obj); } 
  • Instances of functional interfaces can be created with lambda expressions, method references, or constructor references. For example,
                // Before Java8
Runnable runnable1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("runnable1");
}
});
runnable1.run();
// Java8
Runnable runnable2 = () -> System.out.println("runnable2");
runnable2.run();   
  • In java 8, to declare functional interfaces there is a new annotation is introduced called @FunctionalInterface. By using this annotation we will ensure that the functional interface will not have more than one abstract method. If we try to add one more abstract method it will raises the compile timer error. 
                Example 1: 
                @FunctionalInterface 
                interface Greeting {
              public void greeting(String message);
                }
                The above Greeting functional interface is valid interface because it contains only one abstract method. 

                Example 2: 
                @FunctionalInterface 
                interface Animal {
               public void speak();
               public default void name() {
            System.out.println("Hay I am animal");
               }
                }
                The above Animal functional interface is valid because in functional interface we can keep n number of default methods.

               Example 3:
                @FunctionalInterface 

                interface Test {
              public void doWork();
              public boolean equals(Object obj);
              public String toString();
                }
                The above Test functional interface is valid because in function interface we can declare the abstract methods from the java.lang.Object class.
               
                Example 4:
                @FunctionalInterface 

                interface EmptyInterface {
                      
                }
                The above EmptyInterface functional interface is not valid because it does not contain any abstract method.

                Example 5:
                @FunctionalInterface 

                interface MultipleMethodsInterface {
              public void doWork();
              public boolean equals();
                }
                The above MultipleMethodsInterface functional interface is not valid because it contains more than one abstract method.

                Example 6:
                @FunctionalInterface 

                interface Greeting {
              public void sayGreeting();
              public boolean equals();
                }


                interface AnotherGreeting extends Greeting {
              public boolean equals();
                }

                The above AnotherGreeting functional interface is valid because it is inheriting sayGreeting() method and this sayGreeting() method is abstract.

                Example 7:
                @FunctionalInterface 

                interface Greeting2 {
              public void sayGreeting();
              public boolean equals();
                }


                interface AnotherGreeting2 extends Greeting2 {
              public void sayAnotherGreeting();
              public boolean equals();
                }

                The above AnotherGreeting2 functional interface is not valid because it is inheriting sayGreeting() abstract method and also it contains one more abstract method say sayAnotherGreeting() method.
  • The major benefit of functional interface is that we can use lambda expressions to instantiate them and avoid using bulky anonymous class implementation.
  • The advantage of functional interface over normal interface is we can define method definition inside Interface using default modifier which helps to modify existing Interface with out breakage of application.
  • Java 8 Collections API has rewritten and new Stream API is provided that uses a lot of functional interfaces. Java 8 has defined a lot of functional interfaces in java.util.function package, some of the useful ones are Consumer, Supplier, Function and Predicate etc. You can find more here. How to use above functional interface we can see in Stream API examples.
Happy Learning..!

0 comments: