循环三个数组列表以比较值的有效方法

effective way to loopoever three array lists to compare values

我有两个数组列表对象 orderListproductList 和一个字符串数组列表 customerIdList
我有 ProductInfo POJO 要与 orderList 映射和 cuustomerId 应该匹配的 productList。
如果我没有给定 ProdId 的订单或产品列表,我应该添加标准错误并映射到 ProductInfo 错误。

这就是我正在做...

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;
}

样本结果...

{
    "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":[]  
    }
}   

遍历 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);
            }
        }
    }
}

我想知道是否有更好的方法,而且我正在使用 ModelMapperSOrder 映射到 Order POJO 和
其他 POJO想知道是否有任何其他有效的模型映射器可用。
谢谢。

您可以使用 customerId 作为键从 productListorderList 创建映射

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));

然后只需一个循环,您就可以检查是否有该客户 ID 的产品和订单

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