hincky的主页 hincky的主页
  • 学习笔记

    • Vue笔记
    • Vuepress
    • nginx
  • 语言类

    • java
    • go
    • python
    • 设计模式
  • 框架类

    • Spring
    • Spring Security
    • Mybatis
  • 容器技术

    • docker
    • k8s
    • helm
    • prometheus
    • grafana
    • jenkins
  • 命令集合

    • linux命令
    • docker命令
    • git命令
    • vim命令
    • k8s命令
  • 数据库

    • sql
    • mysql
  • 协议

    • 网络模型
    • http/1.1
    • WebSocket
    • http/2
    • TLS/SSL
    • tcp
    • IP
    • tcpdump抓包命令
    • wireshark抓包工具
  • 通用

    • Git
  • 技术分享

    • git push/pull总是超时怎么办
    • idea debug技巧
    • postman使用
    • 问题总结
    • idea使用技巧
  • Oauth2

    • Oauth2原理
  • 项目列表

    • redis项目
    • 微服务项目
  • 分类
  • 标签
  • 归档
  • 随笔
GitHub (opens new window)

Hincky

当有趣的人,做想做的事
  • 学习笔记

    • Vue笔记
    • Vuepress
    • nginx
  • 语言类

    • java
    • go
    • python
    • 设计模式
  • 框架类

    • Spring
    • Spring Security
    • Mybatis
  • 容器技术

    • docker
    • k8s
    • helm
    • prometheus
    • grafana
    • jenkins
  • 命令集合

    • linux命令
    • docker命令
    • git命令
    • vim命令
    • k8s命令
  • 数据库

    • sql
    • mysql
  • 协议

    • 网络模型
    • http/1.1
    • WebSocket
    • http/2
    • TLS/SSL
    • tcp
    • IP
    • tcpdump抓包命令
    • wireshark抓包工具
  • 通用

    • Git
  • 技术分享

    • git push/pull总是超时怎么办
    • idea debug技巧
    • postman使用
    • 问题总结
    • idea使用技巧
  • Oauth2

    • Oauth2原理
  • 项目列表

    • redis项目
    • 微服务项目
  • 分类
  • 标签
  • 归档
  • 随笔
GitHub (opens new window)
  • java

    • java并发
      • cpu
      • Thread几个常用方法
      • Thread类和Runnable接口的比较
    • 函数式编程-lambda
      • Lambda表达式
        • 例一
        • 例二
        • 例三
        • 例四
        • 例五
      • 省略规则
      • 高级格式
        • 引用类的静态方法
        • 引用对象的实例方法
        • 引用类的实例方法
        • 构造器引用
    • 函数式编程-stream
      • 案例数据准备
      • 快速入门
        • 需求
        • 实现
      • 常用操作
        • 创建流
        • 中间操作
        • filter
        • map
        • distinct
        • sorted
        • limit
        • skip
        • flatMap
        • 终结操作
        • forEach
        • count
        • max&min
        • collect
        • 查找与匹配
        • anyMatch
        • allMatch
        • noneMatch
        • findAny
        • findFirst
        • reduce归并
      • 注意事项
    • 函数式编程-optional
      • 使用
        • 创建对象
        • 安全消费值
        • 获取值
        • 安全获取值
        • 过滤
        • 判断
        • 数据转换
    • 函数式接口
      • 常见函数式接口
      • 常用的默认方法
        • and
        • or
        • negate
    • 函数式编程高级用法
      • 基本数据类型优化
      • 并行流
    • IO流-文件
      • 介绍
      • 文件
      • 文件操作
        • 创建文件
        • 获取文件信息
  • python

  • Spring

  • SpringMVC

  • SpringSecurity

  • Mybatis

  • 设计模式

  • Go

  • 后端
  • java
hincky
2022-11-10
目录

函数式接口

概述

​只有一个抽象方法的接口我们称之为函数接口。

​JDK的函数式接口都加上了**@FunctionalInterface** 注解进行标识。但是无论是否加上该注解只要接口中只有一个抽象方法,都是函数式接口。

# 常见函数式接口

  • ​ Consumer 消费接口

    根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中对传入的参数进行消费。

    image-20211028145622163

  • ​ Function 计算转换接口

    根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中对传入的参数计算或转换,把结果返回

    image-20211028145707862

  • ​ Predicate 判断接口

    根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中对传入的参数条件判断,返回判断结果

    image-20211028145818743

  • ​ Supplier 生产型接口

    根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中创建对象,把创建好的对象返回

image-20211028145843368

# 常用的默认方法

# and

我们在使用Predicate接口时候可能需要进行判断条件的拼接。而and方法相当于是使用&&来拼接两个判断条件

例如:

打印作家中年龄大于17并且姓名的长度大于1的作家。

List<Author> authors = getAuthors();
Stream<Author> authorStream = authors.stream();
authorStream.filter(new Predicate<Author>() {
    @Override
    public boolean test(Author author) {
        return author.getAge()>17;
    }
}.and(new Predicate<Author>() {
    @Override
    public boolean test(Author author) {
        return author.getName().length()>1;
    }
})).forEach(author -> System.out.println(author));
1
2
3
4
5
6
7
8
9
10
11
12
13

# or

我们在使用Predicate接口时候可能需要进行判断条件的拼接。而or方法相当于是使用||来拼接两个判断条件。

例如:

打印作家中年龄大于17或者姓名的长度小于2的作家。

//        打印作家中年龄大于17或者姓名的长度小于2的作家。
List<Author> authors = getAuthors();
authors.stream()
        .filter(new Predicate<Author>() {
            @Override
            public boolean test(Author author) {
                return author.getAge()>17;
            }
        }.or(new Predicate<Author>() {
            @Override
            public boolean test(Author author) {
                return author.getName().length()<2;
            }
        })).forEach(author -> System.out.println(author.getName()));
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# negate

Predicate接口中的方法。negate方法相当于是在判断添加前面加了个! 表示取反

例如:

打印作家中年龄不大于17的作家。

//        打印作家中年龄不大于17的作家。
List<Author> authors = getAuthors();
authors.stream()
        .filter(new Predicate<Author>() {
            @Override
            public boolean test(Author author) {
                return author.getAge()>17;
            }
        }.negate()).forEach(author -> System.out.println(author.getAge()));
1
2
3
4
5
6
7
8
9
编辑 (opens new window)
#java
函数式编程-optional
函数式编程高级用法

← 函数式编程-optional 函数式编程高级用法→

最近更新
01
人生前期重要的能力
05-17
02
防火墙命令
04-11
03
docker-compose部署mysql主从集群
03-22
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Hincky | MIT License | 粤ICP备2022120427号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式