未读代码 未读代码
首页
  • Java 18 新功能介绍
  • Java 17 新功能介绍
  • Java 16 新功能介绍
  • Java 15 新功能介绍
  • Java 14 新功能介绍
  • Java 8 新特性

    • Java 8 Lambda 表达式
    • Java 8 Stream 流式操作
    • Java 8 时间处理介绍
    • Java 8 Optional 介绍
  • Java 开发工具
Java 源码分析
Spring Boot 系列
  • Arthas 问题定位
  • JMH 基准测试
GitHub (opens new window)
首页
  • Java 18 新功能介绍
  • Java 17 新功能介绍
  • Java 16 新功能介绍
  • Java 15 新功能介绍
  • Java 14 新功能介绍
  • Java 8 新特性

    • Java 8 Lambda 表达式
    • Java 8 Stream 流式操作
    • Java 8 时间处理介绍
    • Java 8 Optional 介绍
  • Java 开发工具
Java 源码分析
Spring Boot 系列
  • Arthas 问题定位
  • JMH 基准测试
GitHub (opens new window)
  • Java 新特性

  • Java8 新特性

    • Java 8 Lambda 和 Comparator 排序
    • Java 8 Optional 介绍
    • Java 8 Lambda 表达式介绍
    • Java 8 Stream 流式操作
    • Java 8 LocalDate、LocalDateTime 时间处理介绍
    • Java 8 List 转 Map
    • Java 8 Function 函数接口
    • Java 8 Supplier 函数接口
      • 1. Supplier
      • 2. 工厂模式
      • 3. Supplier 扩展类
      • 参考
    • Java 8 Consumer 函数接口
    • Java 8 Predicate 函数接口
    • Java 8 forEach 遍历
    • Java 8 BiFunction 函数接口
    • Java 8 BiPredicate 函数接口
    • Java 8 UnaryOperator 函数接口
  • Java 新特性
  • Java8 新特性
程序猿阿朗
2021-07-23

Java 8 Supplier 函数接口

这篇文章属于 Java 8 教程(LTS)系列教程,点击阅读更多相关文章。

Java 8 中的 Supplier 是一个函数接口,无参数,返回值类型为泛型 T。Supplier 的使用比较简单,使用场景也比较单一。

源码:Supplier 函数接口在 Java 8 中的实现。

package java.util.function;

@FunctionalInterface
public interface Supplier<T> {

    T get();
}
1
2
3
4
5
6
7

# 1. Supplier

Supplier 由于没有参数输入,所以多用于对象创建,类似于一个对象创建工厂。可以使用 Lambda 方式创建任意对象,也可以使用对象构造方法的方法引用创对象。

示例1:使用 Supplier 获取一个 1 到 10 的随机数,使用 Supplier 获取当前时间

import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;
import java.util.function.Supplier;

public class Java8Supplier {

    public static void main(String[] args) {
        Supplier<Integer> supplier = () -> new Random().nextInt(10);
        System.out.println(supplier.get());
        System.out.println(supplier.get());
       
        Supplier<LocalDateTime> supplier2 = LocalDateTime::now;
        System.out.println(supplier2.get());
        System.out.println(supplier2.get());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

输出结果:

9
4
2021-07-21T20:53:36.848264
2021-07-21T20:53:36.848893
1
2
3
4

# 2. 工厂模式

示例:利用 Supplier 构造一个工厂模式,创建不同类别的狗。

import java.util.function.Supplier;

public class Java8SupplierFactory {

    public static void main(String[] args) {
        Dog dog1 = dogFactory(() -> new Dog("哈士奇"));
        Dog dog2 = dogFactory(() -> new Dog("牧羊犬"));
        System.out.println(dog1);
        System.out.println(dog2);
    }

    public static Dog dogFactory(Supplier<? extends Dog> supplier) {
        Dog dog = supplier.get();
        dog.setAge(1);
        return dog;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

源码 Dog.java

public class Dog {
    private String name;
    private Integer age;

    public Dog() {
    }

    public Dog(String name) {
        this.name = name;
    }

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    // get set toString...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

输出结果:

Dog{name='哈士奇', age=1}
Dog{name='牧羊犬', age=1}
1
2

# 3. Supplier 扩展类

在 Java 8 中,为了方便 Supplier 的使用,提供了指定类型的 Supplier,有 BooleanSupplier, DoubleSupplier, IntSupplier, LongSupplier。

示例:使用 IntSupplier 获取 0 到 10 的随机数。

import java.util.Random;
import java.util.function.IntSupplier;

public class Java8SupplierInt {

    public static void main(String[] args) {
        IntSupplier intSupplier = () -> new Random().nextInt(10);
        System.out.println(intSupplier.getAsInt());
        System.out.println(intSupplier.getAsInt());
    }
}
1
2
3
4
5
6
7
8
9
10
11

输出结果:

5
1
1
2

# 参考

  • Supplier (Java Platform SE 8 ) (opens new window)

扩展阅读:Java 8 Predicate 函数接口,Java 8 Function 函数接口

订阅

文章持续更新,订阅可以关注「 程序猿阿朗 」公众号或者未读代码博客。

文章作者: 程序猿阿朗
文章链接:https://www.wdbyte.com/java8/java8-supplier/
版权声明:本网站当前文章采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 未读代码!
#Java8#Function Interface#Java8 Supplier
上次更新: 2022/12/05, 08:18:32
Java 8 Function 函数接口
Java 8 Consumer 函数接口

← Java 8 Function 函数接口 Java 8 Consumer 函数接口→

最近更新
01
如何搭建一个自己的音乐服务器
12-04
02
JUnit 5 单元测试教程
11-17
03
使用 StringUtils.split 的坑
11-02
更多文章>

提示:评论前请刷新页面,否则评论的可能不是当前文章。

Theme by Vdoing | Copyright © 2018-2022 程序猿阿朗 | MIT License | 皖ICP备20000567号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式