用另一个列表扩展 ImmutableList.of()
Extend ImmutableList.of() by another List
我想简化与 ImmutableList.of() 功能相关的现有代码
我已经尝试通过消除 "new..." 构造函数来优化第二个列表的创建,但是当然我不能通过调用 .add();
来扩展不可变列表
当前代码:
static final ImmutableList<ProductCodeEnum> PRODUCTS = ImmutableList.of(ProductCodeEnum.A, ProductCodeEnum.B, ProductCodeEnum.C);
static final ImmutableList<ProductCodeEnum> PRODUCTS_EXTENDED_LIST = new ImmutableList.Builder<ProductCodeEnum>().addAll(PRODUCTS)
.add(ProductCodeEnum.D)
.add(ProductCodeEnum.E)
.build();
预期代码如下:
static final ImmutableList<ProductCodeEnum> PRODUCTS = ImmutableList.of(ProductCodeEnum.A, ProductCodeEnum.B, ProductCodeEnum.C);
static final ImmutableList<ProductCodeEnum> PRODUCTS_EXTENDED = PRODUCTS + ImmutableList.of(ProductCodeEnum.D, ProductCodeEnum.E);
我想你用的是 Guava ImmutableList?
在这种情况下,您的代码将如下所示:
static final ImmutableList<ProductCodeEnum> PRODUCTS = ImmutableList.of(ProductCodeEnum.A, ProductCodeEnum.B, ProductCodeEnum.C);
static final ImmutableList<ProductCodeEnum> PRODUCTS_EXTENDED = ImmutableList.builder().addAll(PRODUCTS).add(ProductCodeEnum.D, ProductCodeEnum.E).build();
我想简化与 ImmutableList.of() 功能相关的现有代码
我已经尝试通过消除 "new..." 构造函数来优化第二个列表的创建,但是当然我不能通过调用 .add();
来扩展不可变列表当前代码:
static final ImmutableList<ProductCodeEnum> PRODUCTS = ImmutableList.of(ProductCodeEnum.A, ProductCodeEnum.B, ProductCodeEnum.C);
static final ImmutableList<ProductCodeEnum> PRODUCTS_EXTENDED_LIST = new ImmutableList.Builder<ProductCodeEnum>().addAll(PRODUCTS)
.add(ProductCodeEnum.D)
.add(ProductCodeEnum.E)
.build();
预期代码如下:
static final ImmutableList<ProductCodeEnum> PRODUCTS = ImmutableList.of(ProductCodeEnum.A, ProductCodeEnum.B, ProductCodeEnum.C);
static final ImmutableList<ProductCodeEnum> PRODUCTS_EXTENDED = PRODUCTS + ImmutableList.of(ProductCodeEnum.D, ProductCodeEnum.E);
我想你用的是 Guava ImmutableList?
在这种情况下,您的代码将如下所示:
static final ImmutableList<ProductCodeEnum> PRODUCTS = ImmutableList.of(ProductCodeEnum.A, ProductCodeEnum.B, ProductCodeEnum.C);
static final ImmutableList<ProductCodeEnum> PRODUCTS_EXTENDED = ImmutableList.builder().addAll(PRODUCTS).add(ProductCodeEnum.D, ProductCodeEnum.E).build();