This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24226|Reply: 1

[Source] Detailed explanation of common functional interfaces (delegates) in Java

[Copy link]
Posted on 9/16/2021 9:28:21 AM | | | |
The JDK 1.8 API includes many built-in functional interfaces, such as the Comparator or Runnable interfaces commonly used in old Java, which add @FunctionalInterface annotations so that they can be used on lambdas. Now, let's really understand it from the entrance of common functions in Function.

nametypedescription
ConsumerConsumer< T >Receives T objects, does not return values
PredicatePredicate< T >Receives the T object and returns the boolean
FunctionFunction< T, R >Receives T object and returns R object
SupplierSupplier< T >Provide T objects (e.g. plants) without accepting values
UnaryOperatorUnaryOperatorReceives T object and returns T object
BinaryOperatorBinaryOperatorReceive two T objects and return T objects


An interface labeled as a FunctionalInterface is called a functional interface, which can only have one custom method, but can include methods inherited from the object class. If an interface has only one method, the compiler will think that it is a functional interface. Whether it is a functional interface or not, the following points need to be noted:


  • The annotation can only be marked on interfaces with "There is only one abstract method".
  • The static and default methods in the JDK8 interface are not abstract methods.
  • The interface inherits java.lang.Object by default, so if the interface shows that the declaration overrides the method in the Object, it is not considered an abstract method.
  • The annotation is not required, and if an interface meets the definition of a "functional interface", it has no effect whether the annotation is added or not. Plus the annotation allows the compiler to check better. If you write a non-functional interface, but add @FunctionInterface, the compiler will report an error.
  • Defining two custom methods in one interface will result in an Invalid '@FunctionalInterface' annotation; FunctionalInterfaceTest is not a functional interface error.


java.util.function It contains a number of classes to support Java's functional programming, and the functional interfaces in this package are:

serial number
Interface & Description
1BiConsumer<T,U>
Represents an action that accepts two input parameters and returns no results
2BiFunction<T,U,R>
Represents a method that accepts two input parameters and returns a result
3BinaryOperator<T>
represents an action that acts on two operators of the same type, and returns the result of the same type of operator
4BiPredicate<T,U>
Represents a two-parameter boolean value method
5BooleanSupplier
Represents the provider of the boolean value result
6Consumer<T>
Represents an operation that accepts an input parameter and returns nothing
7DoubleBinaryOperator
represents an action that acts on two double-valued operators, and returns the result of a double-value.
8DoubleConsumer
Represents an action that accepts a double value parameter and does not return a result.
9DoubleFunction<R>
Represents a method that accepts a double-valued parameter and returns the result
10DoublePredicate
Represents a boolean value method with a double value parameter
11DoubleSupplier
Represents the provider of a double-value structure
12DoubleToIntFunction
Accepts a double-type input and returns an int-type result.
13DoubleToLongFunction
Accepts a double-type input and returns a long-type result
14DoubleUnaryOperator
Accept a parameter of the same type double, and the return value type is also double.
15Function<T,R>
Accepts an input parameter, returns a result.
16IntBinaryOperator
Accept both parameters of type int, and return value type int.
17IntConsumer
Accepts an input parameter of type int with no return value.
18IntFunction<R>
Accepts an int type input parameter and returns a result.
19IntPredicate
: Accepts an int input parameter and returns a boolean result.
20IntSupplier
No parameters, returns an int type result.
21IntToDoubleFunction
Accepts an int type input and returns a double type result.
22IntToLongFunction
Accepts an int type input and returns a long type result.
23IntUnaryOperator
Accept a parameter of type int, and return the value type is also int.
24LongBinaryOperator
Accept both parameters of type long and return value type as long.
25LongConsumer
Accept an input parameter of type long with no return value.
26LongFunction<R>
Accepts a long type input parameter and returns a result.
27LongPredicate
R accepts a long input parameter and returns a boolean type result.
28LongSupplier
No parameter, returns a result of type long.
29LongToDoubleFunction
Accepts a long type input and returns a double type result.
30LongToIntFunction
Accepts a long type input and returns an int type result.
31LongUnaryOperator
Accept a parameter of the same type as long, and return the value of type is also long.
32ObjDoubleConsumer<T>
Accept an object type and a double type input parameter with no return value.
33ObjIntConsumer<T>
Accept input parameters of type object and type int with no return value.
34ObjLongConsumer<T>
Accepts input parameters of type object and type long without returning values.
35Predicate<T>
Accepts an input parameter and returns a Boolean result.
36Supplier<T>
No parameters, returns a result.
37ToDoubleBiFunction<T,U>
Accepts two input parameters and returns a double-type result
38ToDoubleFunction<T>
Accepts an input parameter and returns a double-type result
39ToIntBiFunction<T,U>
Accept two input parameters and return an int type result.
40ToIntFunction<T>
Accept an input parameter and return an int type result.
41ToLongBiFunction<T,U>
Accept two input parameters and return a long-type result.
42ToLongFunction<T>
Accept an input parameter and return a long type result.
43UnaryOperator<T>
Accept a parameter of type T and return the value of type T.


IThe functional interface in Java is the delegate in .NET/C#, pass the method as a parameter, as follows:

Introduction to C# delegates (delegate, Action, Func, predicate)
https://www.itsvse.com/thread-4658-1-1.html

Function Common Methods && Practices

//将Function对象应用到输入的参数上,然后返回计算结果。
R apply(T t);

Although Function< T, R > receive T objects, return R objects, and only one receive and return objects, but combined with tuples, multiple incoming and returning values can be achieved, refer to:


Example 1: Pass two values and sum them

You can use plain writing and lambda expressions, the code is as follows:





Example 2:

andThen method



compose method



Full code



Return results



(End)




Previous:Java FeignClient Interface exception solution for multiple identical service names
Next:maven project \target\classes\META-INF\MANIFEST. MF (system...
Posted on 9/19/2021 7:34:22 PM |
java also looked, although it is not used
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com