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

Java 8中如何创建键可为null的Map<ProductKey, ProductDetail>

问题:构建ProductKey与ProductDetail的映射并为Product赋值

需求说明

  • 为一批未包含ProductDetail的Product列表创建Map<ProductKey, ProductDetail>
  • ProductDetail对象通过ProductDetailLookUpService从数据库读取
  • 部分Product可能没有对应的ProductDetail
  • 同类Product可共享同一个ProductDetail

基础类定义

@Data
static class ProductKey{
  @EqualsAndHashCode.Include
  private Long productCode;
  @EqualsAndHashCode.Include
  private Long productDetailCode;
}
 
@Data
class Product {
  @EqualsAndHashCode.Include
  private ProductKey productKey;
  @EqualsAndHashCode.Include
  private ProductDetail productDetail;
}

@Data
class ProductDetail{
  @EqualsAndHashCode.Include
  private Long productCode;
  private String description;
  private BigDecimal price;
  private String category;
}

public static Optional<ProductKey> findProductKey(Long productCode, 
  List<ProductKey> productKeys){
 return  productKeys.stream()
          .filter(productKey -> productCode.equals(productKey.getProductCode()))
          .takeWhile(productKey -> productKey != null)
          .findFirst();
}

遇到的问题

  • 无法正确处理无ProductDetail时的潜在null值
  • 代码中productKey.getProductCode()方法未被识别

尝试实现的代码

public static void main(String[] args) {
  // 基于数据库配置获取ProductDetails的服务
  ProductDetailLookUpService service = ....
  List<ProductKey> productKeyList  = service.findProductKeys();
  List<Long> productCodes = Arrays.toList(new Long []{23334L, 667777L, 55009L});
  List<ProductDetail> productDetailList = service.getProductDetailList(productCodes);
  Map<Long, ProductDetail> productDetailMap = mapProductCodeToProductDetail(productDetailList);
 
  // 初始状态下Product没有ProductDetails
  // 不同Product可共享同一个ProductDetail
  // 未定义ProductDetail的Product会在productDetailMap中返回null
 
  Map<ProductKey, ProductDetail> map = 
    productKeyList.stream().collect(toMap(productKey -> {
      Optional<ProductKey> optKey = findProductKey(productKey.getProductCode(), productKeyList);
      return optKey.isEmpty()? null : optKey.get(); 
    }, productDetailMap.get(productKey.getProductCode())));
}

public static Optional<ProductKey> findProductKey(Long productCode, List<ProductKey> productKeys){
  return  productKeys.stream()
          .filter(productKey -> productCode.equals(productKey.getProductCode()))
          .takeWhile(productKey -> productKey != null)
          .findFirst();
}

预期的Lambda实现代码

Map<ProductKey, ProductDetail> hashMap = new HashMap<>();

productKeyList.stream().forEach( productKey -> {
  Optional<ProductKey> optKey = findProductKey(productKey.getProductCode(), productKeyList);
  if(optKey.isPresent()) {
    hashMap.put(optKey.get(), productDetailMap.get(optKey.get()));
  }
});

最终为Product设置ProductDetail的代码

List<Product> products = service.readProducts();

products.stream().map( product -> {                                     
  product.setProductDetail(hashMap.get(product.getProductKey()));
  return product;
}).collect(toList());

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 02:15:36