jdk8函数式接口——Consumer介绍
Consumer介绍与实例分析
- 函数式接口:@FunctionalInterface
- Consumer(消费者)
函数式接口:@FunctionalInterface
自从jdk8提供了函数式接口这一新的特性,极大地精简了java开发的方式。而在jdk8之前的版本,函数仅仅只能传递参数,而不能将一个函数或者说是行为传递过去,这意味着我们在调用某个函数时,该函数所表示的执行功能已经很明确了,对于lambda表达式来说,函数的调用,是将函数的行为传递过去,真正执行的是调用时传递的行为。@FunctionalInterface注解是标识一个接口是函数式接口。那么什么样的接口是函数式接口呢?
下面是@FunctionalInterface的注释说明:
Conceptually, a functional interface has exactly one abstract
method. Since {@linkplain java.lang.reflect.Method#isDefault()
default methods} have an implementation, they are not abstract. If
an interface declares an abstract method overriding one of the
public methods of {@code java.lang.Object}, that also does
<em>not</em> count toward the interface's abstract method count
since any implementation of the interface will have an
implementation from {@code java.lang.Object} or elsewhere.
Note that instances of functional interfaces can be created with
lambda expressions, method references, or constructor references.
该注释说了,一个函数式接口应该只有一个抽象方法,对于default methods,是有一个实现,所以它们不是抽象的,这里就说明了jdk8的接口支持方法的实现。如果一个接口声明了一个抽象方法,该方法是被Object类给重写的,那么它不会为该接口的抽象方法增加,因为在Object或者别处会有一个具体的实现。函数式接口的实例可以通过lambda表达式,方法引用,构造方法引用的方式创建出来。这里我们就理解了函数式接口和lambda表达式之间的关系了。下面我主要讲解一个函数式接口Consumer的用法。
Consumer(消费者)
对于Consumer这个接口,我们来看一下它提供的抽象方法是什么?
/*** Performs this operation on the given argument.** @param t the input argument*/void accept(T t);
accept(T t),接受一个参数,没有返回值。举一个例子:
List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9);// 通过lambda表达式构造出Consumer对象list.forEach(i -> System.out.println(i));
这里是通过lambda表达式构造出Consumer对象,将list中每一个元素,传递给consumer,执行打印的操作,我再把这个例子做一个变化
// 通过lambda表达式构造出Consumer对象list.forEach(i -> System.out.println(i * 2));
这里打印的元素是乘2后的结果,这就说明了通过lambda表达式,我们传递的是行为,accept(T t)方法只负责接收一个参数,至于要做什么,是我们再调用的时候,把行为传递过去。
另外还可以使用方法引用的方式来调用Consumer的accept方法。
// 通过方法引用的方式构造出Consumer对象list.forEach(System.out::println);
这里也可以实现遍历每一个元素并打印出来,这是通过方法引用的方式来构造出的Consumer对象。"::"这里两个连续的冒号,是jdk8支持的语法,可以自动定位到具体的函数式接口,这里就可以自动定位到Consumer。
Consumer中还提供了一个默认方法,andThen,来看一下
/*** Returns a composed {@code Consumer} that performs, in sequence, this* operation followed by the {@code after} operation. If performing either* operation throws an exception, it is relayed to the caller of the* composed operation. If performing this operation throws an exception,* the {@code after} operation will not be performed.** @param after the operation to perform after this operation* @return a composed {@code Consumer} that performs in sequence this* operation followed by the {@code after} operation* @throws NullPointerException if {@code after} is null*/default Consumer<T> andThen(Consumer<? super T> after) {Objects.requireNonNull(after);return (T t) -> { accept(t); after.accept(t); };}
该方法默认实现,它接收一个Consumer对象,同时会返回一个Consumer对象,返回的Consumer对象还可以继续调用andThen方法,这样该方法就实现了将执行操作给串行化。举个例子:
public static void main(String[] args) {ConsumerTest02 test = new ConsumerTest02();List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9);test.print(list, item -> System.out.print(" consumer1:" + item * 2), item -> System.out.print(" consumer2:" + item * 3));}/*andThen方法, 将参数传递给调用者执行accept方法,然后再传给第二个consumer执行accept方法。*/public void print(List<Integer> list, IntConsumer con1, IntConsumer con2) {list.forEach(item -> con1.andThen(con2).accept(item));}
该示例构造了两个Consumer对象,通过consumer的andThen方法,将两个操作给串行起来,对于list中每个元素,都会先执行con1的appect方法,再执行con2的accept方法。
打印结果:
consumer1:2 consumer2:3 consumer1:4 consumer2:6 consumer1:6 consumer2:9 consumer1:8 consumer2:12 consumer1:10 consumer2:15 consumer1:12 consumer2:18 consumer1:14 consumer2:21 consumer1:16 consumer2:24 consumer1:18 consumer2:27
关于Consumer这一函数式接口的介绍大致就这些。
jdk8函数式接口——Consumer介绍相关推荐
- [彻底理解]JDK1.8 函数式接口 Consumer Supplier 以及 JAVA新纪元 λ表达式的到来
JDK1.8 函数式接口 Consumer & Supplier 以及 JAVA新纪元 λ表达式的到来 背景什么的被吞了,直接进入主题 函数式接口(定义自己百度,一大堆) 因为看了一些关于JD ...
- 常用的函数式接口--Consumer接口
Consumer java.util.function.Consumer 接口刚好和Supplier接口相反,它不是用来生产一个数据,而是消费一个数据 数据的类型由泛型来指定 accept方法 意思就 ...
- java accept consumer_Java 常用函数式接口 —— Consumer接口
JDK提供了大量的函数式接口,方便我们开发的时候无需自己编写接口,这些接口都比较通用,学会他们并且在工作中使用,不仅方便的解决问题,而且十分优雅. 1.接口概述 Consumer 接口也比较简单,只有 ...
- 常用函数式接口-Consumer
consumer 英 [kənˈsjuːmə(r)] 美 [kənˈsuːmər] n. 消费者:用户,顾客 java.util.function.Consumer<T> 接口则正好与Su ...
- Java基础二十二:函数式接口介绍,函数式接口作为方法参数、返回值,Supplier接口、Consumer接口、Predicate接口、Function接口基本介绍及其案例小练习
函数式接口 1. 函数式接口概念及基本使用 1.函数式接口:有且仅有一个抽象方法的接口,函数式接口就是Lambda表达式使用的前提 2. Java中的函数式编程体现的就是Lambda表达式,所以函数式 ...
- Java 8 - 03 Lambda 函数式接口Predicate Consumer Function Supplier
文章目录 Pre Predicate 断言型函数式接口 Consumer 消费型函数式接口 Function 功能型函数式接口 Supplier 供给型函数式接口 小结 函数式接口如何处理异常信息 P ...
- 学习笔记之-java8的新特性-函数式接口,lambda表达式,方法引用,Stream API,Optional类
1.Lambda表达式 用匿名内部类的方法去创建多线程1.new Thread2.参数传递new Runnable3.重写run方法4.在run方法中去设置线程任务5.调用start问题:我们最终目标 ...
- 未公开接口主要指以下哪几类_Java8的 Stream 函数式接口,你了解多少?
点击蓝色"程序职场"关注我哟 加个"星标",天天和你一起进步 作者:litesky www.jianshu.com/p/2338cabc59e1 函数式接口是伴 ...
- 玩转Java8的 Stream 之函数式接口
作者:litesky www.jianshu.com/p/2338cabc59e1 函数式接口是伴随着Stream的诞生而出现的,Java8Stream 作为函数式编程的一种具体实现,开发者无需关注怎 ...
最新文章
- python垃圾分类源代码_Python学习:怕乱丢垃圾罚款?使用 Python 轻松完成垃圾分类...
- 第一行代码:以太坊(1)-创建自己的私有区块链
- 程序员面试金典 - 面试题 17.23. 最大黑方阵(DP)
- silverlight 不可
- mini_magick上传图片
- 4步搞定MySQL安装部署(附MySQL一键式部署脚本)
- 如何能自动上传公众号文章到网站里面!
- 在RedHat中安装新字体
- Java通过SSH实现文件上传下载功能
- 双栏显示的时候三线格的线太长怎么办?
- 【icourse163】学习python爬虫的代码整理
- android开机自动打开微信小程序,微信小程序新动态:可从手机桌面直接启动
- 计算机网络:家庭无线网组建方案
- 英文论文评审意见_sci英文论文审稿意见怎么写(7)
- redis主从结构 (一主一从,一主多从,主从从)
- 拼多多店铺怎么布置装修,订单才能暴涨?
- linux 错误信息不输出
- 计算机pe教程,u启动windows7PE工具箱
- 量化评价和质化评价举例_量化评价与质性评价的特点和联系
- 顺丰速运和顺丰快递_携手快递外卖小哥,石浦交警送上“安全大礼包”