java8 supplier_Java8 Supplier接口和Consumer接口原理解析
Supplier接口
package java.util.function;
/**
* Represents a supplier of results.
*
*
There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
*
This is a functional interface
* whose functional method is {@link #get()}.
*
* @param the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
supplier接口只有一个抽象方法get(),通过get方法产生一个T类型实例。
实例:
package me.yanand;
import java.util.function.Supplier;
public class TestSupplier {
public static void main(String[] args) {
Supplier appleSupplier = Apple::new;
System.out.println("--------");
appleSupplier.get();
}
}
class Apple{
public Apple() {
System.out.println("创建实例");
}
}
Consumer接口
package java.util.function;
import java.util.Objects;
/**
* Represents an operation that accepts a single input argument and returns no
* result. Unlike most other functional interfaces, {@code Consumer} is expected
* to operate via side-effects.
*
*
This is a functional interface
* whose functional method is {@link #accept(Object)}.
*
* @param the type of the input to the operation
*
* @since 1.8
*/
@FunctionalInterface
public interface Consumer {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
/**
* 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 andThen(Consumer super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
一个抽象方法accept(T t)定义了要执行的具体操作;注意看andThen方法,接收Consumer super T>类型参数,返回一个lambda表达式,此表达式定义了新的执行过程,先执行当前Consumer实例的accept方法,再执行入参传进来的Consumer实例的accept方法,这两个accept方法接收都是相同的入参t。
实例:
package me.yanand;
import java.util.function.Consumer;
public class TestConsumer {
public static void main(String[] args) {
Consumer consumer = (t) -> {
System.out.println(t*3);
};
Consumer consumerAfter = (s) -> {
System.out.println("之后执行:"+s);
};
consumer.andThen(consumerAfter).accept(5);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
java8 supplier_Java8 Supplier接口和Consumer接口原理解析相关推荐
- JAVA8之函数式编程Supplier接口和Consumer接口
JAVA8之函数式编程Supplier接口和Consumer接口 一. Supplier接口 顾名思义,这是一个供应商,提供者.就如一个工厂一样.该类的源码如下: package java.ut ...
- Java基础二十二:函数式接口介绍,函数式接口作为方法参数、返回值,Supplier接口、Consumer接口、Predicate接口、Function接口基本介绍及其案例小练习
函数式接口 1. 函数式接口概念及基本使用 1.函数式接口:有且仅有一个抽象方法的接口,函数式接口就是Lambda表达式使用的前提 2. Java中的函数式编程体现的就是Lambda表达式,所以函数式 ...
- 函数式接口 BiConsumer接口和Consumer接口
BiConsumer定义了两个泛型类型T和R,分别做为accept()方法的参数类型,BiConsumer支持2个参数. 1.void accept(T t, U u);方法 自定义IBiConsum ...
- 128、函数接口类---Consumer
一.概念 java.util.function.Consumer<T>接口正好与Supplier接口相反,它不是生产一个数据,而是 消费一个数据,其数据类型有泛型决定.Consum ...
- Consumer接口
Consumer接口 java.util.function.Consumer 接口则正好与Supplier接口相反,它不是生产一个数据,而是消费一个数据, 其数据类型由泛型决定. 1.抽象方法:acc ...
- 常用的函数式接口--Consumer接口
Consumer java.util.function.Consumer 接口刚好和Supplier接口相反,它不是用来生产一个数据,而是消费一个数据 数据的类型由泛型来指定 accept方法 意思就 ...
- 常用函数式接口:Consumer、Predicate、Function的方法说明解练习
目录 一.常用函数式接口:Consumer 二.Consumer接口练习:按要求打印信息 三. 常用函数式接口:Predicate 四.Predicate接口练习:筛选满足条件数据 五.常用函数式接口 ...
- java accept consumer_Java 常用函数式接口 —— Consumer接口
JDK提供了大量的函数式接口,方便我们开发的时候无需自己编写接口,这些接口都比较通用,学会他们并且在工作中使用,不仅方便的解决问题,而且十分优雅. 1.接口概述 Consumer 接口也比较简单,只有 ...
- JAVA8函数之Supplier和Consumer接口使用理解
一. Supplier接口 顾名思义,这是一个供应商,提供者.就如一个工厂一样.该类的源码如下: package java.util.function;@FunctionalInterface ...
最新文章
- Linux多线程的同步-----信号量和互斥锁
- ceph-kvstore-tool 工具使用详解
- Laravel之Eloquent ORM访问器调整器及属性转换
- ssh linux免密登录。。。。生产共钥到另一台主机
- win2008WEB服务器集群实践(转)
- mysql表中的多对多关系表_「一对多」关系型数据库中一对多,多对一,多对多关系(详细) - seo实验室...
- http头部content-type与数据格式
- wxWidgets:wxMemoryOutputStream类用法
- ObjecT4:On-line multiple instance learning (MIL)学习
- Linux基础-2.目录文件的浏览、管理及维护
- 单片机小白学步系列(四) 模拟电路、传统数字电路与单片机
- 【JVM】第三章 垃圾收集机制
- WPF中查看PDF文件 - 基于开源的MoonPdfPanel (无需安装任何PDF阅读器)问题汇总
- ffmpeg_struct: AVRational
- 怎么设置Linux swap分区?方法教程
- 金蝶K3 WISE所有单据数据库内码及描述对照表
- 2009年考研数学一解析pdf
- Outlook邮箱开启POP服务
- 空气质量等级c语言编程,编程小白如何快速处理空气质量数据
- c枚举类型enum例题_c语言之枚举类型(enum)