引言
java 8 引入了函数式编程的支持,其中最核心的部分就是函数式接口(functional interface)。函数式接口是指只包含一个抽象方法的接口,可以用 lambda 表达式或方法引用来实现。java 8 提供了许多内置的函数式接口,其中最常用的四个是:
consumer<t>:消费型接口
supplier<t>:供给型接口
function<t, r>:函数型接口
predicate<t>:断言型接口
本文将详细介绍这四大函数式接口的使用场景、方法定义以及实际示例,帮助你更好地理解和使用它们。
一、函数式接口简介
1.1 什么是函数式接口?
函数式接口是只有一个抽象方法的接口。java 8 通过@functionalinterface
注解来标识函数式接口。虽然这个注解不是强制性的,但建议使用它来确保接口符合函数式接口的定义。
@functionalinterface public interface myfunctionalinterface { void dosomething(); // 只有一个抽象方法 }
1.2 为什么需要函数式接口?
函数式接口的主要目的是支持 lambda 表达式和方法引用,从而简化代码,使代码更加简洁和易读。通过函数式接口,我们可以将行为(方法)作为参数传递,从而实现更灵活的编程。
二、四大函数式接口详解
2.1consumer<t>:消费型接口
2.1.1 定义
consumer<t>
表示接受一个输入参数并返回无结果的操作。它的抽象方法是:
void accept(t t);
2.1.2 使用场景
适用于需要对输入参数进行处理但不需要返回结果的场景,例如打印、修改对象状态等。
2.1.3 示例
import java.util.function.consumer; public class consumerexample { public static void main(string[] args) { // 使用 lambda 表达式实现 consumer consumer<string> printconsumer = (s) -> system.out.println(s); printconsumer.accept("hello, consumer!"); // 使用方法引用实现 consumer consumer<string> printconsumerref = system.out::println; printconsumerref.accept("hello, method reference!"); } }
2.1.4 其他方法
andthen(consumer<? super t> after)
:组合多个consumer
,按顺序执行。
consumer<string> first = (s) -> system.out.println("first: " + s); consumer<string> second = (s) -> system.out.println("second: " + s); first.andthen(second).accept("hello");
2.2supplier<t>:供给型接口
2.2.1 定义
supplier<t>
表示一个无参数但返回一个结果的供给操作。它的抽象方法是:
t get();
2.2.2 使用场景
适用于需要生成或提供数据的场景,例如工厂方法、延迟计算等。
2.2.3 示例
import java.util.function.supplier; public class supplierexample { public static void main(string[] args) { // 使用 lambda 表达式实现 supplier supplier<string> stringsupplier = () -> "hello, supplier!"; system.out.println(stringsupplier.get()); // 使用方法引用实现 supplier supplier<double> randomsupplier = math::random; system.out.println(randomsupplier.get()); } }
2.3function<t, r>:函数型接口
2.3.1 定义
function<t, r>
表示接受一个输入参数并返回一个结果的函数。它的抽象方法是:
r apply(t t);
2.3.2 使用场景
适用于需要将一个值转换为另一个值的场景,例如数据转换、映射操作等。
2.3.3 示例
import java.util.function.function; public class functionexample { public static void main(string[] args) { // 使用 lambda 表达式实现 function function<string, integer> lengthfunction = (s) -> s.length(); system.out.println(lengthfunction.apply("hello, function!")); // 使用方法引用实现 function function<string, string> uppercasefunction = string::touppercase; system.out.println(uppercasefunction.apply("hello")); } }
2.3.4 其他方法
compose(function<? super v, ? extends t> before)
:组合多个function
,先执行before
,再执行当前函数。andthen(function<? super r, ? extends v> after)
:组合多个function
,先执行当前函数,再执行after
。
function<string, integer> lengthfunction = string::length; function<integer, string> tostringfunction = object::tostring; function<string, string> composedfunction = lengthfunction.andthen(tostringfunction); system.out.println(composedfunction.apply("hello")); // 输出 "5"
2.4predicate<t>:断言型接口
2.4.1 定义
predicate<t>
表示一个接受输入参数并返回布尔值的断言。它的抽象方法是:
boolean test(t t);
2.4.2 使用场景
适用于需要判断输入参数是否满足条件的场景,例如过滤、验证等。
2.4.3 示例
import java.util.function.predicate; public class predicateexample { public static void main(string[] args) { // 使用 lambda 表达式实现 predicate predicate<string> lengthpredicate = (s) -> s.length() > 5; system.out.println(lengthpredicate.test("hello")); // false system.out.println(lengthpredicate.test("hello, predicate!")); // true // 使用方法引用实现 predicate predicate<string> isemptypredicate = string::isempty; system.out.println(isemptypredicate.test("")); // true } }
2.4.4 其他方法
and(predicate<? super t> other)
:逻辑与操作。or(predicate<? super t> other)
:逻辑或操作。negate()
:逻辑非操作。
predicate<string> lengthpredicate = (s) -> s.length() > 5; predicate<string> containspredicate = (s) -> s.contains("hello"); predicate<string> combinedpredicate = lengthpredicate.and(containspredicate); system.out.println(combinedpredicate.test("hello, world!")); // true
三、总结
java 8 的四大函数式接口(consumer
、supplier
、function
、predicate
)为函数式编程提供了强大的支持。通过它们,我们可以用 lambda 表达式和方法引用简化代码,实现更灵活和高效的编程。
consumer<t>
:用于处理输入参数,无返回值。supplier<t>
:用于生成数据,无输入参数。function<t, r>
:用于将输入参数转换为输出结果。predicate<t>
:用于判断输入参数是否满足条件。
到此这篇关于java 8四大函数式接口的定义及使用的文章就介绍到这了,更多相关java8函数式接口内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论