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

JUC并发编程——四大函数式接口(基于狂神说的学习笔记)

四大函数式接口

函数式接口:只有一个方法的接口 ,例如:Runnable接口

Function

函数型接口,有一个输入参数,有一个输出

源码:

/*** Represents a function that accepts one argument and produces a result.** This is a functional interface* whose functional method is apply(Object).** @param <T> the type of the input to the function* @param <R> the type of the result of the function** @since 1.8*/
@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);

示例:

package function;import java.util.function.Function;/**** Function 函数型接口,有一个输入参数,有一个输出* 只要是函数式接口,就可以用lambda表达式*/
public class Demo01 {public static void main(String[] args) {// 匿名内部类,工具类,输出输入的结果
//        Function function = new Function<String,String>() {
//            @Override
//            public String apply(String s) {
//
//                return null;
//            }
//        };// 使用lambda表达式Function function = (str)->{return str;};System.out.println(function.apply("abc"));}
}

Predicate

断定型接口:只有一个输入参数,返回值为boolean

源码:

/*** Represents a predicate (boolean-valued function) of one argument.** This is a functional interface* whose functional method is test(Object).** @param <T> the type of the input to the predicate** @since 1.8*/
@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);

示例

package function;import java.util.function.Predicate;/**** 断定型接口:有一个输入参数,返回值为boolean*/
public class Demo02 {public static void main(String[] args) {// 判断字符串是否为空
//        Predicate predicate = new Predicate<String>() {
//            @Override
//            public boolean test(String s) {
//                return s.isEmpty();
//            }
//        };// 函数型接口+lambda表达式,使代码看起来更加简洁Predicate<String> predicate = (s)->{return s.isEmpty();};System.out.println(predicate.test(""));}
}

Consumer

消费型接口,有一个参数,没有返回值

源码:

/*** 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 afunctional interface* whose functional method is accept(Object).** @param <T> the type of the input to the operation** @since 1.8*/
@FunctionalInterface
public interface Consumer<T> {/*** Performs this operation on the given argument.** @param t the input argument*/void accept(T t);

示例

package function;import javax.lang.model.element.NestingKind;
import java.util.function.Consumer;/**** Consumer 消费型接口:只有输入,没有返回值*/
public class Demo03 {public static void main(String[] args) {
//        Consumer<String> consumer = new Consumer<String>() {
//            @Override
//            public void accept(String s) {
//                System.out.println(s);
//            }
//        };Consumer<String> consumer = (s)->{System.out.println(s);};consumer.accept("asd");}
}

Supplier

供给型接口:没有参数,只有返回值

源码:

/*** 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 isget().** @param <T> the type of results supplied by this supplier** @since 1.8*/
@FunctionalInterface
public interface Supplier<T> {/*** Gets a result.** @return a result*/T get();
}

示例

package function;import java.util.function.Supplier;/**** 供给型接口:没有参数,只有返回值*/
public class Demo04 {public static void main(String[] args) {
//        Supplier<Integer> supplier = new Supplier<Integer>() {
//
//            @Override
//            public Integer get() {
//                return 1024;
//            }
//        };Supplier<Integer> supplier  = ()->{return 1024;};System.out.println(supplier.get());}
}

为什么要学习函数式接口?

  • 简化编程模型,使代码更加可读易懂

  • 在新版本的框架底层中,函数式接口有大量的应用

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

相关文章:

  • 【2】c++11新特性(稳定性和兼容性)—>超长整型 long long
  • AI算法检测对无人军用车辆的MitM攻击
  • 运维 | 如何在 Linux 系统中删除软链接 | Linux
  • Jmeter接口测试:jmeter导入和导出接口的处理
  • 一文了解 Go fmt 标准库的常用占位符及其简单使用
  • Linux命令(94)之history
  • Prompt 驱动架构设计:探索复杂 AIGC 应用的设计之道?
  • 【代码随想录】算法训练营 第三天 第二章 链表 Part 1
  • winform开发经验(1)——调用Invoke更新UI时程序卡死原因以及解决办法
  • JNI 的数据类型以及和Java层之间的数据转换
  • EFLK与logstash过滤
  • docker jenkins
  • 单例模式之「双重校验锁」
  • 2023年中国商业版服务器操作系统市场发展规模分析:未来将保持稳定增长[图]
  • BIM如何通过3D开发工具HOOPS实现WEB轻量化?
  • Unity 3D基础——通过四元数控制对象旋转
  • python--短路运算,把0、空字符串和None看成 False,其他数值和非空字符串都看成 True
  • 《算法通关村第一关——链表青铜挑战笔记》
  • 【深度学习实验】循环神经网络(四):基于 LSTM 的语言模型训练
  • IOS课程笔记[1-3] 第一个IOS应用
  • Flink的基于两阶段提交协议的事务数据汇实现
  • 树模型(三)决策树
  • vueday01——使用属性绑定+ref属性定位获取id
  • LeetCode 260. 只出现一次的数字 III:异或
  • 使用PyTorch解决多分类问题:构建、训练和评估深度学习模型
  • 基于nodejs+vue网课学习平台
  • 读书笔记:Effective C++ 2.0 版,条款13(初始化顺序==声明顺序)、条款14(基类有虚析构)
  • flutter开发实战-下拉刷新与上拉加载更多实现
  • 旧手机热点机改造成服务器方案
  • 网工实验笔记:策略路由PBR的应用场景