Java 8之函数式编程(Function、Consumer、Supplier、Predicate)

本文详细介绍了Java 8中四个重要的函数式接口:Function、Consumer、Supplier和Predicate。Function接口用于表示接受一个参数并产生结果的函数;Consumer接口代表一个接受单一输入参数且不返回结果的操作;Supplier接口则提供了无参数获取结果的功能;Predicate接口定义了一个带有单个参数的布尔值函数,常用于条件判断。每个接口的定义、调用方法和实际应用案例均有详述。
摘要由CSDN通过智能技术生成

1、Function

定义
public interface Function <T, R>

Represents a function that accepts one argument and produces a result.
(表明接受一个参数和产生一个结果的function)

T: the type of the input to the function(入参类型)

R: the type of the result of the function(出参类型)

调用函数
R apply(T t);
使用举例
// 入参+1
Function<Integer, Integer> incrFunc = p -> p + 1;

// ret=7
Integer ret = incrFunc.apply(6);

2、Consumer

定义
public interface Consumer<T>

Represent an operation that accepts a single input argument and returns no result
(表明接受一个参数无返回结果的operation,通常用于处理意外情况或额外动作)

T: the type of the input to the function(入参类型)

调用函数
voi
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值