📜  Java8 函数接口

📅  最后修改于: 2020-10-13 01:57:07             🧑  作者: Mango

Java功能接口

完全包含一种抽象方法的接口称为功能接口。它可以具有任意数量的默认静态方法,但只能包含一个抽象方法。它还可以声明对象类的方法。

功能接口也称为单一抽象方法接口或SAM接口。它是Java中的新功能,有助于实现功能编程方法。

例子1

@FunctionalInterface
interface sayable{
void say(String msg);
}
public class FunctionalInterfaceExample implements sayable{
public void say(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
FunctionalInterfaceExample fie = new FunctionalInterfaceExample();
fie.say("Hello there");
}
}

输出:

Hello there

功能接口可以具有对象类的方法。请参见以下示例。

例子2


@FunctionalInterface
interface sayable{
void say(String msg);// abstract method
// It can contain any number of Object class methods.
int hashCode();
String toString();
boolean equals(Object obj);
}
public class FunctionalInterfaceExample2 implements sayable{
public void say(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
FunctionalInterfaceExample2 fie = new FunctionalInterfaceExample2();
fie.say("Hello there");
}
}

输出:

Hello there

功能接口无效

功能接口只有在没有任何抽象方法时才能扩展另一个接口。

interface sayable{
void say(String msg);// abstract method
}
@FunctionalInterface
interface Doable extends sayable{
// Invalid '@FunctionalInterface' annotation; Doable is not a functional interface
void doIt();
}

输出:

compile-time error

例子3

在以下示例中,功能接口扩展为非功能接口。

interface Doable{
default void doIt(){
System.out.println("Do it now");
}
}
@FunctionalInterface
interface Sayable extends Doable{
void say(String msg);// abstract method
}
public class FunctionalInterfaceExample3 implements Sayable{
public void say(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
FunctionalInterfaceExample3 fie = new FunctionalInterfaceExample3();
fie.say("Hello there");
fie.doIt();
}
}

输出:

Hello there
Do it now

Java预定义功能接口

Java通过使用lambda和方法引用提供了预定义的函数接口来处理函数编程。

您还可以定义自己的自定义功能接口。以下是放置在java.util中的功能接口列表。函数包。

Interface Description
BiConsumer It represents an operation that accepts two input arguments and returns no result.
Consumer It represents an operation that accepts a single argument and returns no result.
Function It represents a function that accepts one argument and returns a result.
Predicate It represents a predicate (boolean-valued function) of one argument.
BiFunction It represents a function that accepts two arguments and returns a a result.
BinaryOperator It represents an operation upon two operands of the same data type. It returns a result of the same type as the operands.
BiPredicate It represents a predicate (boolean-valued function) of two arguments.
BooleanSupplier It represents a supplier of boolean-valued results.
DoubleBinaryOperator It represents an operation upon two double type operands and returns a double type value.
DoubleConsumer It represents an operation that accepts a single double type argument and returns no result.
DoubleFunction It represents a function that accepts a double type argument and produces a result.
DoublePredicate It represents a predicate (boolean-valued function) of one double type argument.
DoubleSupplier It represents a supplier of double type results.
DoubleToIntFunction It represents a function that accepts a double type argument and produces an int type result.
DoubleToLongFunction It represents a function that accepts a double type argument and produces a long type result.
DoubleUnaryOperator It represents an operation on a single double type operand that produces a double type result.
IntBinaryOperator It represents an operation upon two int type operands and returns an int type result.
IntConsumer It represents an operation that accepts a single integer argument and returns no result.
IntFunction It represents a function that accepts an integer argument and returns a result.
IntPredicate It represents a predicate (boolean-valued function) of one integer argument.
IntSupplier It represents a supplier of integer type.
IntToDoubleFunction It represents a function that accepts an integer argument and returns a double.
IntToLongFunction It represents a function that accepts an integer argument and returns a long.
IntUnaryOperator It represents an operation on a single integer operand that produces an integer result.
LongBinaryOperator It represents an operation upon two long type operands and returns a long type result.
LongConsumer It represents an operation that accepts a single long type argument and returns no result.
LongFunction It represents a function that accepts a long type argument and returns a result.
LongPredicate It represents a predicate (boolean-valued function) of one long type argument.
LongSupplier It represents a supplier of long type results.
LongToDoubleFunction It represents a function that accepts a long type argument and returns a result of double type.
LongToIntFunction It represents a function that accepts a long type argument and returns an integer result.
LongUnaryOperator It represents an operation on a single long type operand that returns a long type result.
ObjDoubleConsumer It represents an operation that accepts an object and a double argument, and returns no result.
ObjIntConsumer It represents an operation that accepts an object and an integer argument. It does not return result.
ObjLongConsumer It represents an operation that accepts an object and a long argument, it returns no result.
Supplier It represents a supplier of results.
ToDoubleBiFunction It represents a function that accepts two arguments and produces a double type result.
ToDoubleFunction It represents a function that returns a double type result.
ToIntBiFunction It represents a function that accepts two arguments and returns an integer.
ToIntFunction It represents a function that returns an integer.
ToLongBiFunction It represents a function that accepts two arguments and returns a result of long type.
ToLongFunction It represents a function that returns a result of long type.
UnaryOperator It represents an operation on a single operand that returnsa a result of the same type as its operand.