程序员scholar 程序员scholar
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
npm

(进入注册为作者充电)

  • Java8新特性

    • Java8新特性简介
    • Lambda 表达式
    • 函数式接口
    • 方法的引用
    • 构造器和数组的引用
      • 1. 使用格式
      • 2. 使用要求
        • 2.1 构造器引用
        • 2.2 数组引用
      • 3. 使用举例
        • 3.1 构造器引用
        • 3.2 数组引用
      • 4. 总结
    • Stream 流式编程
    • 并行流与串行流
    • Optional 类的使用
    • 对反射的支持增强
    • 接口中的默认方法与静态方法
    • 新时间日期API
    • Try-with-Resources 升级
    • 重复注解与类型注解
  • Java9新特性

  • Java10新特性

  • Java11新特性

  • Java12新特性

  • Java13新特性

  • Java14新特性

  • Java15新特性

  • Java新特性
  • Java8新特性
scholar
2024-08-24
目录

构造器和数组的引用

# 构造器和数组的引用

前言

在 Java 中,除了方法引用,构造器引用和数组引用也提供了简化代码的方式。它们的使用场景类似于方法引用,尤其适用于实例化对象或创建数组时。下面我将详细介绍构造器引用和数组引用的使用方法。

  • 1. 使用格式
  • 2. 使用要求
    • 2.1 构造器引用
    • 2.2 数组引用
  • 3. 使用举例
    • 3.1 构造器引用
    • 3.2 数组引用
  • 4. 总结

# 1. 使用格式

  • 构造器引用: ClassName::new
  • 数组引用: ArrayType[]::new

# 2. 使用要求

# 2.1 构造器引用

条件: 函数式接口的抽象方法的形参列表和构造器的形参列表一致。抽象方法的返回值类型为构造器所属类的类型。

适用场景: 当我们需要通过某个构造器创建对象,且构造器参数与函数式接口的抽象方法匹配时,可以使用构造器引用来简化代码。

示例: 使用 Supplier、Function 和 BiFunction 结合构造器引用。

# 2.2 数组引用

条件: 当我们需要创建一个数组,且数组的长度可以动态确定时,可以使用数组引用。

适用场景: 在动态创建数组(如流操作中需要生成数组)时,数组引用可以简化代码。

# 3. 使用举例

# 3.1 构造器引用

构造器引用用于简化对象实例化的过程。在以下示例中,我们使用 Supplier、Function 和 BiFunction 结合构造器引用进行对象的创建。

public class ConstructorReferenceExample {

    // Supplier中的T get(),无参构造器
    @Test
    public void testSupplier() {
        // 传统匿名内部类实现
        Supplier<Employee> sup1 = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println("Anonymous Class: " + sup1.get());

        // 使用Lambda表达式
        Supplier<Employee> sup2 = () -> new Employee();
        System.out.println("Lambda: " + sup2.get());

        // 使用构造器引用
        Supplier<Employee> sup3 = Employee::new;
        System.out.println("Constructor Reference: " + sup3.get());
    }

    // Function中的R apply(T t),单参构造器
    @Test
    public void testFunction() {
        // 使用Lambda表达式
        Function<Integer, Employee> func1 = id -> new Employee(id);
        System.out.println("Lambda: " + func1.apply(1001));

        // 使用构造器引用
        Function<Integer, Employee> func2 = Employee::new;
        System.out.println("Constructor Reference: " + func2.apply(1002));
    }

    // BiFunction中的R apply(T t, U u),双参构造器
    @Test
    public void testBiFunction() {
        // 使用Lambda表达式
        BiFunction<Integer, String, Employee> func1 = (id, name) -> new Employee(id, name);
        System.out.println("Lambda: " + func1.apply(1001, "Tom"));

        // 使用构造器引用
        BiFunction<Integer, String, Employee> func2 = Employee::new;
        System.out.println("Constructor Reference: " + func2.apply(1002, "Jerry"));
    }
}

class Employee {
    private Integer id;
    private String name;

    public Employee() {
        this.id = 0;
        this.name = "Default";
    }

    public Employee(Integer id) {
        this.id = id;
        this.name = "Unnamed";
    }

    public Employee(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Employee{id=" + id + ", name='" + name + "'}";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

解释:

  • 函数式接口 Supplier<Employee>: get() 方法无参,因此使用 Employee::new 引用无参构造器。
  • 函数式接口 Function<Integer, Employee>: apply(Integer) 方法的参数为 Integer,因此使用 Employee::new 引用单参构造器。
  • 函数式接口 BiFunction<Integer, String, Employee>: apply(Integer, String) 方法的参数为 Integer 和 String,因此使用 Employee::new 引用双参构造器。

# 3.2 数组引用

数组引用可以用于动态创建数组,特别是在函数式编程中经常需要生成特定长度的数组。

public class ArrayReferenceExample {

    // Function中的R apply(T t),动态创建数组
    @Test
    public void testArrayReference() {
        // 使用Lambda表达式
        Function<Integer, String[]> func1 = length -> new String[length];
        String[] array1 = func1.apply(5);
        System.out.println("Lambda: " + Arrays.toString(array1));

        // 使用数组引用
        Function<Integer, String[]> func2 = String[]::new;
        String[] array2 = func2.apply(10);
        System.out.println("Array Reference: " + Arrays.toString(array2));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

解释:

  • 函数式接口 Function<Integer, String[]>: apply(Integer) 方法的参数为数组长度 Integer,返回值为 String[]。使用 String[]::new 可以动态创建指定长度的字符串数组。

# 4. 总结

  • 构造器引用: 当需要创建对象且构造器参数与函数式接口的抽象方法一致时,可以使用构造器引用来简化代码。常见的场景包括 Supplier、Function 和 BiFunction 等。
  • 数组引用: 当需要动态创建数组时,可以使用数组引用来简化代码。特别是在流操作或需要生成指定长度数组的场景中,数组引用非常有用。

方法引用、构造器引用和数组引用都为我们提供了更加简洁、直观的代码编写方式,适合在函数式编程中使用。

编辑此页 (opens new window)
上次更新: 2024/12/28, 18:32:08
方法的引用
Stream 流式编程

← 方法的引用 Stream 流式编程→

Theme by Vdoing | Copyright © 2019-2025 程序员scholar
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式