我正在嘗試為需要添加 ProductDetail 的產品串列創建 Map<ProductKey, ProductDetail>。ProductDetail 物件是從服務 ProductDetailLookUpService 的資料庫中讀取的。我沒有添加該服務,因為它需要大量接觸內部資訊。產品可能有也可能沒有 ProductDetail,類似的產品可能共享相同的 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();
}
這是我嘗試創建 Map<ProductKey, ProductDetail> 的地圖。當產品沒有 ProductDetail 時,我不知道如何處理潛在的空值。此外,方法 productKey.getProductCode() 似乎無法識別。
這是我的嘗試:
public static void main(String[] args) {
//Service that uses database configuration to obtain ProductDetails based on
//ProductCodes
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);
// At start Product does not have ProductDetails
// Products may share ProductDetails
// Products which do not yet have ProductDetails defined will return a null
// value in the productDetailMap
Map<ProductKey, ProductDetail> map =
productKeyList.stream().collect(toMap(productKey -> {
Optional<ProductKey> optKey =
findProductKey(productKey.getProductCode(),
productKeyList)
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();
}
我想使用 Java 8 lambda 來獲取用于將 ProductDetail 添加到 Product 的地圖。這是我想在下面使用的,以生成 Map<ProductKey, ProductDetail>
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()));
}
});
最后,我想使用地圖將 ProductDetail 添加到 Product 中,如下所示:
List<Product> products = service.readProducts();
products.stream().map( product -> {
product.setProductDetail(hashMap.get(product.getProductKey()));
return product;
}
).collect(toList());
uj5u.com熱心網友回復:
使用 CollectortoMap()時,您至少需要提供兩個引數:
- keyMapper -從流元素生成Key的函式;
- valueMapper -從流元素生成值的函式。
并且有允許您指定mergeFunction和mapFactory的多載版本。如果可以包含重復項,則mergeFunction將很有用productKeyList,因此會有多個元素產生相同的key。
呼叫findProductKey()應該在流中完成(在收集器之前)。你可以放置成一個map()操作。然后過濾掉空的選項,提取值,然后應用collect()。
List<ProductKey> productKeyList = // initilizing the list
Map<Long, ProductDetail> productDetailMap = // initializing the map
Map<ProductKey, ProductDetail> prodDetailByKey = productKeyList.stream()
.map(productKey ->
findProductKey(productKey.getProductCode(), productKeyList)
)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toMap(
Function.identity(),
productKey -> productDetailMap.get(productKey.getProductCode()),
(left, rirgt) -> left // precaution against duplicated elements in the productKeyList
));
為了設定ProductDetail從 Map 到Product具有相應產品密鑰的Iterable.forEach(),將比流更合適。
List<Product> products = service.readProducts();
products.forEach(product ->
product.setProductDetail(prodDetailByKey.get(product.getProductKey()))
);
關于null值,它們可以很容易地從任何地圖中消除:
productDetailMap.values().removeIf(Objects::isNull);
請注意,如果地圖中不存在特定的產品密鑰,則您無能為力。呼叫get()將回傳null,productDetail屬性將保持不變。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/530869.html
上一篇:從班級串列中獲取內容
