2016-10-12 18 views
0

私はarraylistの2つのオブジェクトorderList,productListと1つのストリングarraylist customerIdListを持っています。
私はProductInfo POJOを、customerListと一致するはずのorderListおよびproductListとマッピングする必要があります。
ProdIdの注文または製品リストがない場合は、標準エラーを追加してProductInfoエラーにマップする必要があります。ここ

は、私がどんながあるかどうかを知りたい...私は...値を比較するために3つの配列リストをループする効果的な方法

public class ProductInfo { 
    private List<ProductDetails> products; 
    private List<Error> errors; 
    private String customerId; 
} 

public class ProductDetails { 
    private String customerId; 
    private Order order; 
    private Product product; 
    private List<Error> errors; 
} 

サンプル結果をやって何

のArrayListとマッピング

for(String customerId : customerIdList) { 
    for(Product product: productList) { 
     for(SOrder ordr: orderList) { 
      if(customerId.equals(product.getCustomerId()) && customerId.equals(Order.getCustomerId())) { 
       ModelMapper mapper = new ModelMapper(); 
       Order order = mapper.map(ordr, Order.class)); 
       productDetails.setOrder(order); 
       //mapping to ProductInfo 
       productDetailsList.add(productDetails); 
      } 
     } 
    } 
} 

上でループ

{ 
    "productInfo": { 
     "customer_id": "123", 
     "product_details": [ 
     { 
      "customer_id": "123", 
      "order_details": null, 
      "product_details": { 
       "customer_id": "123" 
       "product_id" : "2343" 
       "product_name": "XYZ", 
       "product_type": "PQR" 
       ... 
      }, 
      "errors": [ 
       "error_code":"6001", 
       "error_desc":"Failure in getting Order information from Order Service" 
      ] 
     }, 
     { 
      "order_details": { 
       "customer_id":"123" 
       "order_id": "3543454", 
       "order_date":"2016-10-12", 
       "order_status":"ordered" 
      }, 
      "product_details": null, 
      "errors": [ 
       "error_code":"6001", 
       "error_desc":"Failure in getting Product information from Product Service" 
      ] 
     } 
     ], 
     "system_errors":[] 
    } 
} 

ですこれを行う良い方法と私もModelMapperSOrderOrder POJOとにマップしています210の他のPOJOは、利用可能な他の効率的なモデルマッパーがあるかどうかを知りたいと思います。
ありがとうございます。

答えて

0

あなたはただ一つのループであなたがその顧客IDの製品と注文

for(String customerId : customerIdList) { 
    if (productMap.containsKey(customerId) && orderMap.containsKey(customerId)) { 
    //do your mapping stuff here 
    } 
} 
+0

がある場合、それはnullをスローし確認することができ、キー

Map<String, Product> productMap = productList.stream().collect(Collectors.toMap(p -> p.getCustomerId(), p -> p)); Map<String, Product> orderMap = orderList.stream().collect(Collectors.toMap(o -> o.getCustomerId(), o -> o)); 

としてcustomerIdproductListorderListからマップを作成することができますプロダクトまたは注文が存在しない場合はポインタ例外 – user1653027

+0

@ user1653027 nullを除くIDを除外します。 .filter(Objects :: nonNull) –

関連する問題