java8 function interface

Code that recently doing code review team found that when someone uses a @FunctionalInterface comment. I am not very familiar with this comment, basically not used. So I asked the corresponding developer, give me talk about the usage of this annotation, why should this comment? He is not talking about full, so I query some information to share to you!

Before speaking @FunctionalInterface, we get to know, what is the function interface (Functional Interface)?

Functional Interface (Functional Interface)

Functional Interface (Functional Interface) is a term for a class of Java 8 special type of interface. Such interface defines only abstract methods of the interface (in addition to the common method implied Object object), so he will have done the very beginning SAM type of interface (Single Abstract Method).

To put it plainly, the so-called functional interfaces, of course, first is an interface, then there is at this interface can have only abstract methods in a non-public methods of the Object object, there can be multiple static methods and the default method.

It says that the concept, if you have not read, it does not matter, we continue by following a few examples, I'm sure you'll understand.

Existing prior to JDK 8 function interface

Prior to JDK 8 JDK already provided support functional programming interface function.

  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.nio.file.PathMatcher
  • java.lang.reflect.InvocationHandler
  • java.beans.PropertyChangeListener
  • java.awt.event.ActionListener
  • javax.swing.event.ChangeListener

Interface functions newly defined in Java8

Java8 java.util.function in a package. Which defines the type of functional groups of interfaces and sub-interfaces for basic data types.

  • Predicate: passing a parameter bool returns a result, the method of boolean test (T t)
  • Consumer: passing a parameter and returns no value, pure consumption. The method is void accept (T t)
  • Function: a pass parameters, return a result, the method of R apply (T t)
  • Supplier: no argument to return a result, the method is T get ()
  • UnaryOperator: unary operator, inheritance Function, parameter passing and return type of the same type.
  • BinaryOperator: binary operator, two types of parameters passed in and return the same type, inheritance BiFunction

Let's look at a case.

1
2
3
4
@FunctionalInterface
public interface XttblogService {
     void sayMessage(String message);
}

So we can now use Lambda expressions to represent an implementation of this interface (Note: JAVA is generally anonymous class implemented before 8):

1
XttblogService xttblogService = message -> System.out.println( "Hello " + message);

@FunctionalInterface annotation interface, only one public interface.

@FunctionalInterface

If you define the two, it will error.

But we can define multiple default method. Because the default method is not an abstract method has a default implementation, it is consistent with the definition of functional interfaces.

1
2
3
4
5
6
7
8
9
@FunctionalInterface
public interface XttblogService{
     void sayMessage(String message);
     default void doSomeMoreWork1(){
         // 业余草:www.xttblog.com
     }
     default void doSomeMoreWork2(){
     }
}

In addition, we can also define multiple static methods.

1
2
3
4
5
6
7
8
9
10
@FunctionalInterface
public interface XttblogService {
     void sayMessage(String message);
     static void printHello(){
         System.out.println( "Hello" );
     }
     static void xttblogHello(){
         System.out.println( "Xttblog Hello" );
     }
}

Function allows you to define the default interface in the methods and static methods, both of the above are written without error.

Further, there is also the function interface allows java.lang.Object defined in public methods.

Function interface in the Object is included in the public methods that are a function interface, it is not as abstract method (although they are abstract methods); because any implementation of a functional interface, default inherit the Object class, including java.lang.Object in the realization of these abstract methods from.

1
2
3
4
5
6
7
8
9
10
11
12
13
@FunctionalInterface
public interface XttblogService  {
     void sayMessage(String message);
 
     @Override
     boolean equals(Object obj);
 
     @Override
     String toString();
 
     @Override
     int hashCode();
}

We used some of the interface Callable, Runnable, Comparator, etc. are added in JDK8 @FunctionalInterface comment.

So why Java needs @FunctionalInterface comment it?

Without this comment, we can also realize Lambda expressions.

But the reason for the introduction of @FunctionalInterface Java annotations in Java to achieve Lambda, developers do not want to define a special group of Structural type of function as Lambda expressions alone, called the arrow type (arrow type), both still want to use Java type system (class, interface, method, etc.). Add a structured type of function will increase the complexity of the type of function, destroying the existing Java type, and severely affected hundreds of thousands of Java class libraries. On balance, therefore eventually use SAM interface as the target type Lambda expressions.

JDK already in some of the interface itself is functional interfaces, such as Runnable. 8 JDK added java.util.function package, provides a common interface function.

Functional interface represents a contract, and a contract for a particular type of function. Where it appears, we expect a real function in line with contract requirements. Lambda expressions can not exist out of context, it must have a clear target type, and this type of goal is a functional interface.

 

Guess you like

Origin www.cnblogs.com/vana/p/11016349.html