当前位置: 首页 > news >正文

java8四大基本函数式接口

 

1.什么是函数式接口?

  • 只包含一个抽象方法的接口,称为函数式接口
  • 你可以通过Lambda表达式来创建该接口的对象。(若Lambda表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明)
  • 我们可以在任意函数式接口上使用@FunctionalInterface注解,这样做可以检查它是否是一个函数式接口,同时javadoc也会包含一条声明,说明这个接口是一个函数式接口

2.四大函数式接口的使用

函数接口如果使用的好能让我们的代码变得较为简洁,且减少重复代码。

2.1. Consumer<T> 消费型接口,有入参,无返回值

源码:

@FunctionalInterface
public interface Consumer<T> {/*** Performs this operation on the given argument.** @param t the input argument*/void accept(T t);
}

例子 设置属性name和nameDesc的值:

    public static void main(String[] args) {List<UserInfo> userInfoList = getUserList();userInfoList.forEach(obj -> {functionTest(obj::setName);functionTest(obj::setNameDesc);System.out.println(obj);});}public static void functionTest(Consumer<String> consumer){consumer.accept("name");}

2.2 Function<T, R> 函数型接口 有入参 有返回值

源码:


@FunctionalInterface
public interface Function<T, R> {/*** Applies this function to the given argument.** @param t the function argument* @return the function result*/R apply(T t);}

例子 获取属性nameDesc的值:

    public static void main(String[] args) {List<UserInfo> userInfoList = getUserList();userInfoList.forEach(obj -> {functionTest(obj, UserInfo::getNameDesc);});}public static void functionTest(UserInfo userInfo, Function<UserInfo, String> function){String apply = function.apply(userInfo);System.out.println(apply);
}

2.3 Supplier<T> 提供型接口 空参但是有返回值

源码:


@FunctionalInterface
public interface Supplier<T> {/*** Gets a result.** @return a result*/T get();
}

例子 获取属性nameDesc的值:

    public static void main(String[] args) {List<UserInfo> userInfoList = getUserList();userInfoList.forEach(obj -> {functionTest(obj::getNameDesc);});}public static void functionTest(Supplier<String> supplier){String s = supplier.get();}

2.4 Predicat<T> 断言型接口 返回真假

源码:


@FunctionalInterface
public interface Predicate<T> {/*** Evaluates this predicate on the given argument.** @param t the input argument* @return {@code true} if the input argument matches the predicate,* otherwise {@code false}*/boolean test(T t);
}

例子 计算一个数字是否大于100: 

    public static void main(String[] args) {functionTest(50, x -> x > 100);}public static void functionTest(int x, Predicate<Integer> predicate){boolean test = predicate.test(x);System.out.println(test);}

3.其他函数式接口

序号接口描述
1BiConsumer<T,U>代表了一个接受两个输入参数的操作,并且不返回任何结果
2BiFunction<T,U,R>代表了一个接受两个输入参数的方法,并且返回一个结果
3BinaryOperator<T>

代表了一个作用于于两个同类型操作符的操作,并且返回了

操作符同类型的结果

4BiPredicate<T,U>代表了一个两个参数的boolean值方法
5BooleanSupplier代表了boolean值结果的提供方
6Consumer<T>代表了接受一个输入参数并且无返回的操作
7DoubleBinaryOperator

代表了作用于两个double值操作符的操作,并且返回了

一个double值的结果

8DoubleConsumer代表一个接受double值参数的操作,并且不返回结果。
9DoubleFunction<R>代表接受一个double值参数的方法,并且返回结果
10DoublePredicate代表一个拥有double值参数的boolean值方法
11DoubleSupplier代表一个double值结构的提供方
12DoubleToIntFunction接受一个double类型输入,返回一个int类型结果
13DoubleToLongFunction接受一个double类型输入,返回一个long类型结果
14DoubleUnaryOperator接受一个参数同为类型double,返回值类型也为double
15Function<T,R>接受一个输入参数,返回一个结果。
16IntBinaryOperator接受两个参数同为类型int,返回值类型也为int 
17IntConsumer接受一个int类型的输入参数,无返回值 
18IntFunction<R>接受一个int类型输入参数,返回一个结果 。
19IntPredicate接受一个int输入参数,返回一个布尔值的结果
20IntSupplier无参数,返回一个int类型结果
21IntToDoubleFunction接受一个int类型输入,返回一个double类型结果 
22IntToLongFunction接受一个int类型输入,返回一个long类型结果
23IntUnaryOperator接受一个参数同为类型int,返回值类型也为int 
24LongBinaryOperator接受两个参数同为类型long,返回值类型也为long
25LongConsumer接受一个long类型的输入参数,无返回值
26LongFunction<R>接受一个long类型输入参数,返回一个结果
27LongPredicateR接受一个long输入参数,返回一个布尔值类型结果
28LongSupplier无参数,返回一个结果long类型的值
29LongToDoubleFunction接受一个long类型输入,返回一个double类型结果
30LongToIntFunction接受一个long类型输入,返回一个int类型结果
31LongUnaryOperator接受一个参数同为类型long,返回值类型也为long
32ObjDoubleConsumer<T>接受一个object类型和一个double类型的输入参数,无返回值
33ObjIntConsumer<T>接受一个object类型和一个int类型的输入参数,无返回值
34ObjLongConsumer<T>接受一个object类型和一个long类型的输入参数,无返回值
35Predicate<T>接受一个输入参数,返回一个布尔值结果
36Supplier<T>无参数,返回一个结果
37ToDoubleBiFunction<T,U>接受两个输入参数,返回一个double类型结果
38ToDoubleFunction<T>接受一个输入参数,返回一个double类型结果
39ToIntBiFunction<T,U>接受两个输入参数,返回一个int类型结果
40ToIntFunction<T>接受一个输入参数,返回一个int类型结果
41ToLongBiFunction<T,U>接受两个输入参数,返回一个long类型结果
42ToLongFunction<T>接受一个输入参数,返回一个long类型结果
43UnaryOperator<T>接受一个参数为类型T,返回值类型也为T

http://www.lryc.cn/news/30727.html

相关文章:

  • Junit测试框架
  • 操作系统复习题
  • web项目的初始化
  • 29- 迁移学习 (TensorFlow系列) (深度学习)
  • 工具篇(五)炫酷排版,尽在LaTeX:让你的文档飞升吧!
  • 【蓝桥杯PythonB组备赛】【Acwing周赛】第93场 4867. 整除数 4868. 数字替换 python解
  • KNN学习报告
  • Java奠基】方法的讲解与使用
  • 字符串hash
  • 试题 算法训练 转圈游戏
  • 【uni-app教程】九、运行环境判断与跨端兼容
  • 扩展WSL2虚拟硬盘的大小
  • Win系统蓝牙设备频繁卡顿/断连 - 解决方案
  • Git学习入门(2)- 基本命令操作总结
  • SPringCloud:Nacos快速入门及相关属性配置
  • 医疗器械之模糊算法(嵌入式部分)
  • 网上销售笔记本系统
  • MySQL基础查询操作
  • English Learning - L2 语音作业打卡 小元音 [ʌ] [ɒ] Day9 2023.3.1 周三
  • Condition 源码解读
  • 看完这篇入门性能测试
  • 推导部分和——带权并查集
  • 费解的开关/翻硬币
  • OpenGL中的坐标系
  • Spring——Spring介绍和IOC相关概念
  • A+B Problem
  • 【ROS学习笔记11】ROS元功能包与launch文件的使用
  • 【python】
  • 充电协议: 快充协议,如何选充电宝?
  • 视觉SLAM十四讲ch6 非线性优化笔记