java8 function interface (Functional Interface)

Introduction

Functional Interface (Functional Interface) is one and only one abstract method, but there may be a plurality of non-abstract methods of the interface.

Interface function can be implicitly converted to lambda expression (arrow function).

Functional interface represents a contract, and a contract for a particular type of function.
Lambda expressions can not exist out of context, it must have a clear objective type (interface), and this target type is a function interface.

Before java8 existing interfaces have many functions, such as java.lang.Runnable, java.util.concurrent.Callable, java.util.Comparator like.
Newly added functions in the interface are java.util.function package.

Java is not mandatory that you use @FunctionalInterface annotation to mark your interface is a function interface, however, as the API author, you may prefer to use @FunctionalInterface indicate a specific interface for the function interface, this is just a design considerations, can let users know that a very clear interface is a functional interface.

getting Started

First look at an example entry can understand why functional interface is a contract:

@FunctionalInterface 
public interface the Predicate <T> { 
    // The interface has an abstract method Test, which takes a single parameter t, and returns Boolean 
    // an abstract method so defined, is in agreement, the abstract method implementation class I, it must be only one parameter, and only returns a boolean value of. 
    // function so that the interface is a contract. 
    Test Boolean (T T); 
}

  

Predicate的使用实例:
// method evel takes a list and a Predicate interfaces 
// list as parameters for each element will test methods of the predicate. 
// because the function interface is using a Lambda expression, the second parameter can be used herein function of the arrows. 
static void the eval public (List <Integer> List, the Predicate <Integer> predicate) { 
      // cycle List 
      for (Integer I: List) { 
         // Test method calls predicate returns a boolean value. 
         IF (predicate.test (I)) { 
            System.out.println (I + ""); 
         } 
          // question: Predicate is an interface ah! Where that method (implementation class / implementation) Specifically, he performed in it? ? ? ? 
          // See main method !!! 
      } 
   } 
   
public static void main (String args []) { 
      // definition of a List 
      List <Integer> Arrays.asList List = (. 1, 2,. 3,. 4,. 5,. 6,. 7 ,. 8,. 9); 

      // n--> n-2%    
      // actually very simple, this is actually achieved Lambda expressions Predicate interface: 
      // n arrow on the left is a parameter that is passed to the test method Predicate interface 
      // and the right arrow n% 2 is actually the abstract test the method of implementation, which is used to calculate the input argument is an even number. 
      // So the whole method is used to print a list of even number. 
      the eval (List, N-> n-% 2 == 0); 
      System.out.println ( "Output all even:"); 

   }

 

New function interface

java.util.function defined types of functional groups of interfaces and sub-interfaces for basic data types.

  • Predicate - passing a parameter and returns a bool result, method 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

Method abstract functional interface

In the above description, when it comes to functional interface has one and only one abstract method, in fact, is not correct, because the functional interface can additionally define more abstract methods, but these abstract method signature must be the Object class public methods as , but we generally do not re-define these methods. 

@FunctionalInterface
public interface ObjectMethodFunctionalInterface {
	void count(int i);
	
	String toString(); //same to Object.toString
	int hashCode(); //same to Object.hashCode
	boolean equals(Object obj); //same to Object.equals
}

 

Unusual statement

Abstract method declarations function interface may be abnormalities (checked exception). This exception must catch the target object when calling this method.

@FunctionalInterface
interface InterfaceWithException {
	//声明异常
	void apply(int i) throws Exception;
}

  

Catch exceptions:
public class FunctionalInterfaceWithException {
	public static void main(String[] args) {
		InterfaceWithException target = i -> {};
		try {
			target.apply(10);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Illegal throw an exception:

 
 
@FunctionalInterface 
interface InterfaceWithException { 
	// declare no abnormality 
	void Apply (I int); 
} 
public class FunctionalInterfaceWithException { 
	public static void main (String [] args) { 
		// declare function interface without exception, 
		// but it is thrown in Lambda abnormal, and this was not compile in! ! ! 
		Target = I InterfaceWithException -> the throw new new Exception {();}; 
	} 
}
  
 

Static method

In addition to the interface function abstract method may further comprise the static method.
8 previous specification not permitted in interfaces in Java static method. Static methods can only be defined in the class. But in Java 8, you can define a static method.

The default method

Java 8 allowed in the interface method, rather than a simple declaration of these methods is called the default method, use the special keyword default.
The default method because it is not an abstract method, it does not affect us determine whether an interface is functional interface.

Functional Interface Overview function included in the category of

  1. BiConsumer <T, U> represents an operation that takes two input parameters, and returns no result

  2. BiFunction <T, U, R> represents a method accepts two input parameters, and returns a result

  3. BinaryOperator represents an operation applied to two of the same type on the operator, and returns the result of the operator of the same type

  4. BiPredicate <T, U> boolean value representing the two parameters of a method of

  5. BooleanSupplier boolean value represents the result of the provider

  6. Consumer represents accepts an operation input parameter and a non-return

  7. DoubleBinaryOperator represents two double values ​​applied to the operation of the operator, and returns the results of a double value.

  8. DoubleConsumer represents a double operating parameter value acceptance and returns the result.

  9. Representative DoubleFunction method of receiving a double value of the parameter, and returns the result

  10. On behalf of DoublePredicate a boolean value method has double value of the parameter

  11. DoubleSupplier configuration represents a double value provider

  12. DoubleToIntFunction accepts a double input type, returns a result of type int.

  13. DoubleToLongFunction accepts a double input type, returns a result of type long

  14. DoubleUnaryOperator accepts a parameter of the same type double, the return type is also double.

  15. Function <T, R> accepts an input parameter and returns a result.

  16. IntBinaryOperator accepts two parameters the same type int, also the type of return value to int.

  17. IntConsumer requires an int input parameters, no return value.

  18. IntFunction requires an int input and returns a result.

  19. IntPredicate: accepting an int input parameter and returns a Boolean value of the result.

  20. IntSupplier no parameters and returns a result of type int.

  21. IntToDoubleFunction requires an int input, returns a result of type double.

  22. IntToLongFunction requires an int input, returns a result of type long.

  23. IntUnaryOperator accepts a parameter of the same type int, also the type of return value to int.

  24. LongBinaryOperator accepts two parameters the same type of long, return type is also a long.

  25. LongConsumer accept a long type input parameters, no return value.

  26. LongFunction accepts a long type input parameters and returns a result.

  27. LongPredicate R takes a long input and returns a Boolean result type.

  28. LongSupplier no parameters and returns a result value type long.

  29. LongToDoubleFunction input accepts a long type, returns a result of type double.

  30. LongToIntFunction input accepts a long type, returns a result of type int.

  31. LongUnaryOperator accepts a parameter of the same type long, return type is also a long.

  32. ObjDoubleConsumer accepts an object type and a double input type parameters, no return value.

  33. ObjIntConsumer accepts an object type and an input parameter of type int, no return value.

  34. ObjLongConsumer accepts an object type and a long type of input parameters, no return value.

  35. Predicate accepts an input parameter and returns a boolean result.

  36. Supplier no parameters and returns a result.

  37. ToDoubleBiFunction <T, U> accepts two input parameters and returns a result of type double

  38. ToDoubleFunction accepts an input parameter and returns a result of type double

  39. ToIntBiFunction <T, U> accepts two input parameters and returns a result of type int.

  40. ToIntFunction accepts an input parameter and returns a result of type int.

  41. ToLongBiFunction <T, U> accepts two input parameters and returns a result of type long.

  42. ToLongFunction accepts an input parameter and returns a result of type long.

  43. UnaryOperator accepts a parameter of type T, also the type of return value T


Author: DoubleDragon
link: https: //juejin.im/post/5c77b354e51d457143522e13
Source: Nuggets

Guess you like

Origin www.cnblogs.com/ampl/p/11441129.html