Java 8 函数式接口
原文:https://www.studytonight.com/java-8/java-8-functional-interface
函数式接口是 Java 8 中引入的一个概念。一个只有一个抽象方法并标有 @FunctionalInterface 注解的接口叫做 FunctionalInterface。
函数接口用于支持函数式编程方法、lambda 表达式以及方法引用。
由于单一抽象方法(SAM),函数式接口也被称为 SAM 接口。
让我们看看如何创建一个函数式接口,尽管 Java 提供了各种内置的函数式接口,我们已经在主题末尾的表格中进行了总结。
示例:功能界面
我们正在创建一个用注释标记的函数式接口,它只包含一个抽象方法。
@FunctionalInterface
interface Printable{
public void print();
}
示例:错误的功能界面
如果我们对具有多个抽象方法的接口使用函数接口注释,那么编译器将会产生错误。
@FunctionalInterface
interface Printable{
public void print();
public void print3D();
}
错误: 无效的“@FunctionalInterface”注释;可打印不是功能界面
非抽象方法函数式接口
一个函数接口可以有默认的和静态的方法,就像普通接口一样,但是只有一个抽象方法。下面是一个有效的功能界面。
@FunctionalInterface
interface Printable{
// Abstract method
public void print();
// non-abstract (static) method
public static void print3D() {
System.out.println("3D Printing");
}
}
如何实现一个函数式接口?
函数式接口可以通过使用 lambda 表达式或方法引用来实现。要了解 Lambda 表达式,您可以访问我们的Lambda 表达式主题。
让我们看一些实现函数式接口的例子。
示例:功能界面
在这个例子中,我们使用 lambda 表达式实现了一个函数接口。使用 lambda 表达式在 Demo 类中实现了一个抽象方法 print()。
@FunctionalInterface
interface Printable{
public void print();
public static void print3D() {
System.out.println("3D Printing");
}
}
public class Demo {
public static void main(String[] args) {
// Implementing functional interface using lambda
Printable p = ()->System.out.println("Printing...");
p.print();
}
}
印刷...
示例:使用方法引用的功能界面
在这个例子中,我们使用方法引用实现了一个函数接口。方法引用是一个概念,在这个概念中,我们可以将方法引用到接口的抽象方法。见下面的例子。
@FunctionalInterface
interface Printable{
public void print();
public static void print3D() {
System.out.println("3D Printing");
}
}
public class Demo {
public static void main(String[] args) {
Demo demo = new Demo();
// Referring to a non-functional method
Printable p = demo::printColor;
// calling abstract method
p.print();
}
// Implementation of print() abstract method
public void printColor() {
System.out.println("Color Printing...");
}
}
彩色印刷...
有效:扩展界面
函数式接口只能扩展没有任何抽象方法的非函数式接口。
interface Drawable{
public default void draw() {
System.out.println("Draw Image");
}
}
@FunctionalInterface
interface Printable extends Drawable{
public void print();
public static void print3D() {
System.out.println("3D Printing");
}
}
无效:扩展界面
函数接口不能扩展任何具有一个或多个抽象方法的接口。编译器将在编译时报告错误。
interface Drawable{
public void draw();
}
@FunctionalInterface
interface Printable extends Drawable{
public void print();
public static void print3D() {
System.out.println("3D Printing");
}
}
错误:无效的“@FunctionalInterface”注释;可打印不是功能界面
Java 内置功能界面
Java 提供了各种内置的函数接口来支持函数编程,尽管我们可以像本主题开始时那样创建自己的函数接口。
下表包含位于**Java . util . function包中的函数式接口列表。**
| 界面 | 描述** |
| 双消费者〔t0〕 | 它表示接受两个输入参数并且不返回结果的操作。 |
| 消费者 | 它表示接受单个参数并且不返回结果的操作。 |
| 功能 | 它表示接受一个参数并返回一个结果的函数。 |
| 谓词 | 它表示一个参数的谓词(布尔值函数)。 |
| 双功能 | 它表示接受两个参数并返回一个结果的函数。 |
| 二进制操作器 | 它表示对相同数据类型的两个操作数的操作。它返回与操作数类型相同的结果。 |
| 双重定向 | 它表示两个参数的谓词(布尔值函数)。 |
| BooleanSupplier | 它代表布尔值结果的提供者。 |
| 双二进制运算符 | 它表示对两个Double
类型操作数的操作,并返回一个Double
类型值。 |
| 双重消费者 | 它表示接受单个Double
类型参数并且不返回结果的操作。 |
| 双功能〔t0〕 | 它表示接受Double
类型参数并产生结果的函数。 |
| 双重预测 | 它表示一个Double
类型参数的谓词(布尔值函数)。 |
| 双重供应商 | 它代表了双重结果的供应商。 |
| DoubleToIntFunction | 它表示接受Double
类型参数并产生 int 类型结果的函数。 |
| IntToDoubleFunction | 它表示接受整数参数并返回双精度值的函数。 |
| IntToLongFunction | 它表示接受整数参数并返回 long 类型的函数。 |
| 插管操作器 | 它表示对单个整数操作数的运算,产生整数结果。 |
| LongBinaryOperator | 它表示对两个Long
类型操作数的操作,并返回一个Long
类型结果。 |
| 长期消费者 | 它表示接受单个Long
类型参数并且不返回结果的操作。 |
| 长功能 | 它表示接受Long
类型参数并返回结果的函数。 |
| longpredictate | 它表示一个Long
类型参数的谓词(布尔值函数)。 |
| 长期供应商 | 它代表长型结果的供应商。 |
| LongToDoubleFunction | 它表示接受Long
类型参数并返回Double
类型结果的函数。 |
| LongToIntFunction | 它表示接受Long
类型参数并返回整数结果的函数。 |
| longunaryooperator | 它表示对单个Long
类型操作数的操作,返回Long
类型结果。 |
| object double consumer〔t0〕 | 它表示接受对象和双参数的操作,并且不返回任何结果。 |
| 对象用户〔t0〕 | 它表示接受对象和整数参数的操作。它不返回结果。 |
| ObjLongConsumer | 它表示接受一个对象和一个长参数的操作,它不返回任何结果。 |
| 供应商 | 它代表结果的提供者。 |
| 至双功能 | 它表示一个接受两个参数并产生Double
类型结果的函数。 |
| 全功能〔t0〕 | 它表示一个返回双精度类型结果的函数。 |
| 屋顶分叉〔t0〕 | 它表示接受两个参数并返回一个整数的函数。 |
| 屋顶功能〔t0〕 | 它表示一个返回整数的函数。 |
| ToLongBiFunction | 它表示接受两个参数并返回Long
类型结果的函数。 |
| 托伦函数〔t0〕 | 它表示返回Long
类型结果的函数。 |
| 无操作者 | 它表示对单个操作数的操作,该操作数返回与其操作数类型相同的结果。 |