“函数式接口(JDK8):Function、Consumer、Predicate、Supplier”的版本间差异
跳到导航
跳到搜索
无编辑摘要 |
|||
第2行: | 第2行: | ||
== 关于 == | == 关于 == | ||
JDK 1.8 | JDK 1.8 API包含了很多内建的函数式接口,在老Java中常用到的比如“Comparator”或者“Runnable”接口,这些接口都增加了<big><span style="color: green">“'''@FunctionalInterface'''”</span></big>注解以便能用在'''lambda'''上。 | ||
函数式接口: | 函数式接口: | ||
# 接口“有且仅有一个抽象方法”。 | # 接口“有且仅有一个抽象方法”。 | ||
# 接口中的静态方法和默认方法,都不算是抽象方法。 | #* 接口中的静态方法和默认方法,都不算是抽象方法。 | ||
# 接口默认继承“java.lang. | # 接口默认继承“java.lang.Object”,所以如果接口显示声明覆盖了 Object 中方法,那么也不算抽象方法。 | ||
JDK 8 函数式接口: | |||
{| class="wikitable" | {| class="wikitable" | ||
! 函数式接口 !! 说明 !! 备注 | ! 函数式接口 !! 说明 !! 备注 | ||
|- | |- | ||
| Function< T, R > || 功能型接口 || 接收T对象,返回R对象 | | Function<T, R> || 功能型接口 || 接收T对象,返回R对象 | ||
|- | |- | ||
| Predicate< T > || 谓词型接口 || 接收T对象并返回boolean | | Predicate<T> || 谓词型接口 || 接收T对象并返回boolean | ||
|- | |- | ||
| Consumer< T > || 消费型接口 || 接收T对象,不返回值 | | Consumer<T> || 消费型接口 || 接收T对象,不返回值 | ||
|- | |- | ||
| Supplier< T > || 供给型接口 || 提供T对象(例如工厂),不接收值 | | Supplier<T> || 供给型接口 || 提供T对象(例如工厂),不接收值 | ||
|} | |} | ||
== Function< T, R > == | == Function<T, R> == | ||
Function | Function 接口是一个'''功能型接口''':转换作用,将输入数据转换成另一种形式的输出数据。 | ||
* 通过实现“'''apply'''”方法,来做转换。 | |||
* 其他 Function 接口: | * 其他 Function 接口: | ||
*: IntFunction | *: '''IntFunction''' 、'''DoubleFunction''' 、'''LongFunction''' 、'''ToIntFunction''' 、'''ToDoubleFunction''' 、'''DoubleToIntFunction''' 等等,使用方法一样。 | ||
* | * “'''Function.identity()'''”:返回一个输出跟输入一样的 Lambda 表达式对象,等价于形如“t -> t”形式的Lambda表达式。 | ||
* 源码: | * 源码: | ||
*: <syntaxhighlight lang="java"> | *: <syntaxhighlight lang="java"> | ||
第38行: | 第37行: | ||
@FunctionalInterface | @FunctionalInterface | ||
public interface Function<T, R> { | public interface Function<T, R> { | ||
// | // 接受输入参数,对输入执行所需操作后,返回一个结果。 | ||
R apply(T t); | R apply(T t); | ||
第79行: | 第78行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Predicate< T > == | == Predicate<T> == | ||
Predicate | Predicate 接口是一个'''谓词型接口''':其实就是一个类似于 bool 类型的判断的接口; | ||
* 通过实现“'''test'''”方法,来做判断。 | |||
=== 示例 === | === 示例 === | ||
第111行: | 第110行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Consumer< T > == | == Consumer<T> == | ||
Consumer | Consumer 接口是一个'''消费型接口''':通过传入参数,然后输出值。 | ||
* 通过实现“'''accept'''”方法,来消费数据。 | |||
* 其他 Consumer 接口: | * 其他 Consumer 接口: | ||
*: | *: '''IntConsumer'''、'''DoubleConsumer'''、'''LongConsumer'''、'''BitConsumer''' 等等,使用方法一样。 | ||
=== 示例 === | === 示例 === | ||
第146行: | 第144行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Supplier< T > == | == Supplier<T> == | ||
Supplier | Supplier 接口是一个'''供给型接口''':其实就是一个可以用来存储数据的容器; | ||
* 通过实现“'''get'''”方法,来提供数据。 | |||
=== 示例 === | === 示例 === |
2022年4月23日 (六) 01:07的最新版本
关于
JDK 1.8 API包含了很多内建的函数式接口,在老Java中常用到的比如“Comparator”或者“Runnable”接口,这些接口都增加了“@FunctionalInterface”注解以便能用在lambda上。
函数式接口:
- 接口“有且仅有一个抽象方法”。
- 接口中的静态方法和默认方法,都不算是抽象方法。
- 接口默认继承“java.lang.Object”,所以如果接口显示声明覆盖了 Object 中方法,那么也不算抽象方法。
JDK 8 函数式接口:
函数式接口 | 说明 | 备注 |
---|---|---|
Function<T, R> | 功能型接口 | 接收T对象,返回R对象 |
Predicate<T> | 谓词型接口 | 接收T对象并返回boolean |
Consumer<T> | 消费型接口 | 接收T对象,不返回值 |
Supplier<T> | 供给型接口 | 提供T对象(例如工厂),不接收值 |
Function<T, R>
Function 接口是一个功能型接口:转换作用,将输入数据转换成另一种形式的输出数据。
- 通过实现“apply”方法,来做转换。
- 其他 Function 接口:
- IntFunction 、DoubleFunction 、LongFunction 、ToIntFunction 、ToDoubleFunction 、DoubleToIntFunction 等等,使用方法一样。
- “Function.identity()”:返回一个输出跟输入一样的 Lambda 表达式对象,等价于形如“t -> t”形式的Lambda表达式。
- 源码:
package java.util.function; import java.util.Objects; @FunctionalInterface public interface Function<T, R> { // 接受输入参数,对输入执行所需操作后,返回一个结果。 R apply(T t); // 返回一个 先执行before函数对象apply方法,再执行当前函数对象apply方法的 函数对象。 default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } // 返回一个 先执行当前函数对象apply方法, 再执行after函数对象apply方法的 函数对象。 default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } // 返回一个执行了apply()方法之后只会返回输入参数的函数对象。 static <T> Function<T, T> identity() { return t -> t; } }
示例
- 作为“stream.map()”的参数类型;
@Test
public void test_Function() {
// 使用map方法,泛型的第一个参数是转换前的类型,第二个是转化后的类型
Function<String, Integer> function = new Function<String, Integer>() {
@Override
public Integer apply(String s) {
return s.length();//获取每个字符串的长度,并且返回
}
};
Stream<String> stream = Stream.of("aaa", "bbbbb", "ccccccv");
Stream<Integer> stream1 = stream.map(function);
stream1.forEach(System.out::println);
}
Predicate<T>
Predicate 接口是一个谓词型接口:其实就是一个类似于 bool 类型的判断的接口;
- 通过实现“test”方法,来做判断。
示例
- 作为“stream.filter()”的参数类型;
@Test
public void test_Predicate() {
//1、使用Predicate接口实现方法,只有一个test方法,传入一个参数,返回一个bool值
Predicate<Integer> predicate = new Predicate<Integer>() {
@Override
public boolean test(Integer integer) {
if(integer > 5){
return true;
}
return false;
}
};
System.out.println(predicate.test(6));
//2、 使用lambda表达式,
predicate = (t) -> t > 5;
System.out.println(predicate.test(1));
//3、stream.filter(predicate)
Stream<Integer> stream = Stream.of(1, 23, 3, 4, 5, 56, 6, 6);
List<Integer> list = stream.filter(predicate).collect(Collectors.toList());
list.forEach(System.out::println);
}
Consumer<T>
Consumer 接口是一个消费型接口:通过传入参数,然后输出值。
- 通过实现“accept”方法,来消费数据。
- 其他 Consumer 接口:
- IntConsumer、DoubleConsumer、LongConsumer、BitConsumer 等等,使用方法一样。
示例
- 作为“stream.forEach()”的参数类型;
@Test
public void test_Consumer() {
//1、使用consumer接口实现方法
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
Stream<String> stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
stream.forEach(consumer);
//2、使用lambda表达式,forEach方法需要的就是一个Consumer接口
stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
stream.forEach((s) -> System.out.println(s));
//3、使用方法引用,方法引用也是一个consumer
stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
Consumer consumer2 = System.out::println;
stream.forEach(consumer);
// 等同于:
//stream.forEach(System.out::println);
}
Supplier<T>
Supplier 接口是一个供给型接口:其实就是一个可以用来存储数据的容器;
- 通过实现“get”方法,来提供数据。
示例
- 作为“optional.orElseGet()”的参数类型;
Optional<Integer> first = stream.filter(i -> i > 4).findFirst(); System.out.println(first.orElseGet(supplier)); // orElse:如果first中存在数,就返回这个数,如果不存在,就放回传入的数 // orElseGet:如果first中存在数,就返回这个数,如果不存在,就返回supplier返回的值
@Test
public void test_Supplier() {
//1、使用Supplier接口实现方法,只有一个get方法,无参数,返回一个值
Supplier<Integer> supplier = new Supplier<Integer>() {
@Override
public Integer get() {
//返回一个随机值
return new Random().nextInt();
}
};
System.out.println(supplier.get());
//2、使用lambda表达式,
supplier = () -> new Random().nextInt();
System.out.println(supplier.get());
//3、使用方法引用
Supplier<Double> supplier2 = Math::random;
System.out.println(supplier2.get());
}