如何深入理解JAVA8中的函数式接口详解?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1738个文字,预计阅读时间需要7分钟。
Java8中内置了许多在开发中常用的高效函数式接口,极大提升了我们的开发效率。下面是一些常见的函数式接口:
1. `Consumer`:接收一个参数,不返回结果。
2.`Supplier`:返回一个结果,不接收参数。
3.`Function`:将一个参数转换成另一个结果。
4.`Predicate`:用于测试一个参数是否为真。
5.`BiConsumer`:接收两个参数,不返回结果。
6.`BiFunction`:接收两个参数,返回一个结果。
7.`BinaryOperator`:接收两个相同的参数,返回一个结果。
8.`ToIntFunction`:将一个参数转换为int。
9.`ToLongFunction`:将一个参数转换为long。
10.`ToDoubleFunction`:将一个参数转换为double。
这里,我用表格的形式来概述这些函数式接口:
| 接口名称 | 参数类型 | 返回类型 | 用途 |
| ---------------- | ---------------------- | -------- | ------------------------------------------------------------ || Consumer | T | void | 接收一个参数,不返回结果 || Supplier | 无 | T | 返回一个结果,不接收参数 || Function | T | R | 将一个参数转换成另一个结果 || Predicate | T | boolean | 用于测试一个参数是否为真 || BiConsumer | T, U | void | 接收两个参数,不返回结果 || BiFunction | T, U | R | 接收两个参数,返回一个结果 || BinaryOperator | T, T | T | 接收两个相同的参数,返回一个结果 || ToIntFunction | T | int | 将一个参数转换为int || ToLongFunction | T | long | 将一个参数转换为long || ToDoubleFunction | T | double | 将一个参数转换为double |写在前面
Java8中内置了一些在开发中常用的函数式接口,极大的提高了我们的开发效率。那么,问题来了,你知道都有哪些函数式接口吗?
函数式接口总览
这里,我使用表格的形式来简单说明下Java8中提供的函数式接口。
四大核心函数式接口
首先,我们来看四大核心函数式接口,如下所示。
其他函数接口
除了四大核心函数接口外,Java8还提供了一些其他的函数式接口。
四大核心函数式接口
Consumer接口
1.接口说明
Consumer接口是消费性接口,无返回值。Java8中对Consumer的定义如下所示。
@FunctionalInterface public interface Consumer<T> { void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } }
2.使用示例
public void handlerConsumer(Integer number, Consumer<Integer> consumer){ consumer.accept(number); } @Test public void test1(){ this.handlerConsumer(10000, (i) -> System.out.println(i)); }
Supplier接口
1.接口说明
Supplier接口是供给型接口,有返回值,Java8中对Supplier接口的定义如下所示。
@FunctionalInterface public interface Supplier<T> { T get(); }
2.使用示例
public List<Integer> getNumberList(int num, Supplier<Integer> supplier){ List<Integer> list = new ArrayList<>(); for(int i = 0; i < num; i++){ list.add(supplier.get()) } return list; } @Test public void test2(){ List<Integer> numberList = this.getNumberList(10, () -> new Random().nextInt(100)); numberList.stream().forEach(System.out::println); }
Function接口
1.接口说明
Function接口是函数型接口,有返回值,Java8中对Function接口的定义如下所示。
@FunctionalInterface public interface Function<T, R> { R apply(T t); default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } static <T> Function<T, T> identity() { return t -> t; } }
2.使用示例
public String handlerString(String str, Function<String, String> func){ return func.apply(str); } @Test public void test3(){ String str = this.handlerString("binghe", (s) -> s.toUpperCase()); System.out.println(str); }
Predicate接口
1.接口说明
Predicate接口是断言型接口,返回值类型为boolean,Java8中对Predicate接口的定义如下所示。
@FunctionalInterface public interface Predicate<T> { boolean test(T t); default Predicate<T> and(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } default Predicate<T> negate() { return (t) -> !test(t); } default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } static <T> Predicate<T> isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } }
2.使用示例
public List<String> filterString(List<String> list, Predicate<String> predicate){ List<String> strList = new ArrayList<>(); for(String str : list){ if(predicate.test(str)){ strList.add(str); } } return strList; } @Test public void test4(){ List<String> list = Arrays.asList("Hello", "Lambda", "binghe", "lyz", "World"); List<String> strList = this.filterString(list, (s) -> s.length() >= 5); strList.stream().forEach(System.out::println); }
注意:只要我们学会了Java8中四大核心函数式接口的用法,其他函数式接口我们也就知道如何使用了!
写在最后
最后,附上Java8新特性核心知识图,祝大家在学习Java8新特性时少走弯路。
以上就是详解JAVA8 函数式接口的详细内容,更多关于JAVA8 函数式接口的资料请关注易盾网络其它相关文章!
本文共计1738个文字,预计阅读时间需要7分钟。
Java8中内置了许多在开发中常用的高效函数式接口,极大提升了我们的开发效率。下面是一些常见的函数式接口:
1. `Consumer`:接收一个参数,不返回结果。
2.`Supplier`:返回一个结果,不接收参数。
3.`Function`:将一个参数转换成另一个结果。
4.`Predicate`:用于测试一个参数是否为真。
5.`BiConsumer`:接收两个参数,不返回结果。
6.`BiFunction`:接收两个参数,返回一个结果。
7.`BinaryOperator`:接收两个相同的参数,返回一个结果。
8.`ToIntFunction`:将一个参数转换为int。
9.`ToLongFunction`:将一个参数转换为long。
10.`ToDoubleFunction`:将一个参数转换为double。
这里,我用表格的形式来概述这些函数式接口:
| 接口名称 | 参数类型 | 返回类型 | 用途 |
| ---------------- | ---------------------- | -------- | ------------------------------------------------------------ || Consumer | T | void | 接收一个参数,不返回结果 || Supplier | 无 | T | 返回一个结果,不接收参数 || Function | T | R | 将一个参数转换成另一个结果 || Predicate | T | boolean | 用于测试一个参数是否为真 || BiConsumer | T, U | void | 接收两个参数,不返回结果 || BiFunction | T, U | R | 接收两个参数,返回一个结果 || BinaryOperator | T, T | T | 接收两个相同的参数,返回一个结果 || ToIntFunction | T | int | 将一个参数转换为int || ToLongFunction | T | long | 将一个参数转换为long || ToDoubleFunction | T | double | 将一个参数转换为double |写在前面
Java8中内置了一些在开发中常用的函数式接口,极大的提高了我们的开发效率。那么,问题来了,你知道都有哪些函数式接口吗?
函数式接口总览
这里,我使用表格的形式来简单说明下Java8中提供的函数式接口。
四大核心函数式接口
首先,我们来看四大核心函数式接口,如下所示。
其他函数接口
除了四大核心函数接口外,Java8还提供了一些其他的函数式接口。
四大核心函数式接口
Consumer接口
1.接口说明
Consumer接口是消费性接口,无返回值。Java8中对Consumer的定义如下所示。
@FunctionalInterface public interface Consumer<T> { void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } }
2.使用示例
public void handlerConsumer(Integer number, Consumer<Integer> consumer){ consumer.accept(number); } @Test public void test1(){ this.handlerConsumer(10000, (i) -> System.out.println(i)); }
Supplier接口
1.接口说明
Supplier接口是供给型接口,有返回值,Java8中对Supplier接口的定义如下所示。
@FunctionalInterface public interface Supplier<T> { T get(); }
2.使用示例
public List<Integer> getNumberList(int num, Supplier<Integer> supplier){ List<Integer> list = new ArrayList<>(); for(int i = 0; i < num; i++){ list.add(supplier.get()) } return list; } @Test public void test2(){ List<Integer> numberList = this.getNumberList(10, () -> new Random().nextInt(100)); numberList.stream().forEach(System.out::println); }
Function接口
1.接口说明
Function接口是函数型接口,有返回值,Java8中对Function接口的定义如下所示。
@FunctionalInterface public interface Function<T, R> { R apply(T t); default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } static <T> Function<T, T> identity() { return t -> t; } }
2.使用示例
public String handlerString(String str, Function<String, String> func){ return func.apply(str); } @Test public void test3(){ String str = this.handlerString("binghe", (s) -> s.toUpperCase()); System.out.println(str); }
Predicate接口
1.接口说明
Predicate接口是断言型接口,返回值类型为boolean,Java8中对Predicate接口的定义如下所示。
@FunctionalInterface public interface Predicate<T> { boolean test(T t); default Predicate<T> and(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } default Predicate<T> negate() { return (t) -> !test(t); } default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } static <T> Predicate<T> isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } }
2.使用示例
public List<String> filterString(List<String> list, Predicate<String> predicate){ List<String> strList = new ArrayList<>(); for(String str : list){ if(predicate.test(str)){ strList.add(str); } } return strList; } @Test public void test4(){ List<String> list = Arrays.asList("Hello", "Lambda", "binghe", "lyz", "World"); List<String> strList = this.filterString(list, (s) -> s.length() >= 5); strList.stream().forEach(System.out::println); }
注意:只要我们学会了Java8中四大核心函数式接口的用法,其他函数式接口我们也就知道如何使用了!
写在最后
最后,附上Java8新特性核心知识图,祝大家在学习Java8新特性时少走弯路。
以上就是详解JAVA8 函数式接口的详细内容,更多关于JAVA8 函数式接口的资料请关注易盾网络其它相关文章!

