0

I have two object of arraylist orderList, productList and one String arraylist customerIdList.
I have ProductInfo POJO to be mapped with orderList and productList where cuustomerId should match.
If I don't have order or productlist for given ProdId I should add standard Error and map to the ProductInfo Error.

Here is what I am doing ...

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

Sample result ...

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

Looping over ArrayList and Mapping

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

I want to know if there is any better way of doing this and also I am using ModelMapper to map SOrder to Order POJO and
other POJOs would like to know if there is any other efficient model mapper available.
Thanks.

boraseoksoon
  • 2,164
  • 1
  • 20
  • 25
user1653027
  • 789
  • 1
  • 16
  • 38

1 Answers1

0

you can create maps from productList and orderList with customerId as key

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

then with just one loop you can check if there is product and order for that customer id

for(String customerId : customerIdList) {
  if (productMap.containsKey(customerId) && orderMap.containsKey(customerId)) {
    //do your mapping stuff here
  }
}
Misha
  • 27,433
  • 6
  • 62
  • 78
tonakai
  • 805
  • 1
  • 7
  • 15