You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中使用Builder模式创建List:合并双Builder的实现方案

问题描述

我定义了一个标注@Data和@Builder注解的CustomerRequest类,包含字符串类型属性cutspec与List<Customer>类型属性listOfcustomer。目前需要先通过Customer的Builder创建对象并组装成List,再传入CustomerRequest的Builder构建实例,这种方式不够优雅。请问能否将这两个Builder合并为一个?

当前实现代码:

@Data
@Builder
public class CustomerRequest{
    private String cutspec;
    private List<Customer> listOfcustomer;
}
Customer customer = Customer.builder()
        .conditionType("valueCondition")
        .key("customerNo")
        .operator("=")
        .value(customerId)
        .build();
    
List<Customer> listOfcustomer = new ArrayList<>();
listOfcustomer.add(customer);

return CustomerRequest.builder()
        .cutspec("John")
        .listOfcustomer(listOfcustomer)
        .build();
解决方案

当然可以通过几种方式简化构建流程,让代码更简洁优雅:

方法1:利用Lombok的@Singular注解(推荐)

Lombok的@Singular注解专门用于集合类型属性,会自动为Builder生成添加单个元素的方法,无需手动创建List。

修改CustomerRequest类:

@Data
@Builder
public class CustomerRequest{
    private String cutspec;
    @Singular
    private List<Customer> listOfcustomer;
}

现在可以直接在CustomerRequest的Builder中链式添加Customer对象:

return CustomerRequest.builder()
        .cutspec("John")
        .listOfcustomer(Customer.builder()
                .conditionType("valueCondition")
                .key("customerNo")
                .operator("=")
                .value(customerId)
                .build())
        .build();

添加多个Customer时,重复调用listOfcustomer方法即可:

return CustomerRequest.builder()
        .cutspec("John")
        .listOfcustomer(Customer.builder()
                .conditionType("valueCondition")
                .key("customerNo")
                .operator("=")
                .value(customerId1)
                .build())
        .listOfcustomer(Customer.builder()
                .conditionType("valueCondition")
                .key("customerNo")
                .operator("=")
                .value(customerId2)
                .build())
        .build();

方法2:手动给CustomerRequest的Builder添加自定义方法

如果不想用@Singular,可以手动在Builder中添加单个元素的添加方法:

@Data
@Builder
public class CustomerRequest{
    private String cutspec;
    private List<Customer> listOfcustomer;

    public static class CustomerRequestBuilder {
        public CustomerRequestBuilder addCustomer(Customer customer) {
            if (this.listOfcustomer == null) {
                this.listOfcustomer = new ArrayList<>();
            }
            this.listOfcustomer.add(customer);
            return this;
        }
    }
}

使用时直接调用自定义方法:

return CustomerRequest.builder()
        .cutspec("John")
        .addCustomer(Customer.builder()
                .conditionType("valueCondition")
                .key("customerNo")
                .operator("=")
                .value(customerId)
                .build())
        .build();

方法3:内联创建List(无需修改类)

无需改动原有类结构,直接在CustomerRequest的Builder中内联生成List:

return CustomerRequest.builder()
        .cutspec("John")
        .listOfcustomer(Collections.singletonList(
                Customer.builder()
                        .conditionType("valueCondition")
                        .key("customerNo")
                        .operator("=")
                        .value(customerId)
                        .build()
        ))
        .build();

需要多个元素时,可以用初始化块直接构建:

return CustomerRequest.builder()
        .cutspec("John")
        .listOfcustomer(new ArrayList<>() {{
            add(Customer.builder()
                    .conditionType("valueCondition")
                    .key("customerNo")
                    .operator("=")
                    .value(customerId1)
                    .build());
            add(Customer.builder()
                    .conditionType("valueCondition")
                    .key("customerNo")
                    .operator("=")
                    .value(customerId2)
                    .build());
        }})
        .build();

内容的提问来源于stack exchange,提问作者Loren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 12:03:23