时间:2021-05-20
Supplier接口
package java.util.function;/** * Represents a supplier of results. * * <p>There is no requirement that a new or distinct result be returned each * time the supplier is invoked. * * <p>This is a <a href="package-summary.html" rel="external nofollow" rel="external nofollow" >functional interface</a> * whose functional method is {@link #get()}. * * @param <T> the type of results supplied by this supplier * * @since 1.8 */@FunctionalInterfacepublic interface Supplier<T> { /** * 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<Apple> 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. * * <p>This is a <a href="package-summary.html" rel="external nofollow" rel="external nofollow" >functional interface</a> * whose functional method is {@link #accept(Object)}. * * @param <T> the type of the input to the operation * * @since 1.8 */@FunctionalInterfacepublic interface Consumer<T> { /** * 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<T> 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<Integer> consumer = (t) -> { System.out.println(t*3); }; Consumer<Integer> consumerAfter = (s) -> { System.out.println("之后执行:"+s); }; consumer.andThen(consumerAfter).accept(5); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言在Java8之前,默认情况下,接口中的所有方法都是公共的和抽象的。但是这一限制在Java8中被打破了,Java8允许开发人员在接口中添加新方法,而无需在实现
Java8新特性内建函数式接口 在之前的一片博文Lambda表达式,提到过Java8提供的函数式接口。在此文中,将介绍一下Java8四个最基本的函数式接口
1.函数式接口的理解根据重构的思想,需要把容易变化的模块进行抽象并封装起来,从这个点来看,Java8新引入的函数式接口就是基于这个思想进行设计的。2.函数式接口
前言之前两篇文章分别介绍了Java8的lambda表达式和默认方法和静态接口方法。今天我们继续学习Java8的新语言特性——方法引用(MethodReferen
static方法java8中为接口新增了一项功能:定义一个或者更多个静态方法。用法和普通的static方法一样。接口中可以定义static方法,可通过接口名称.