I want to remove a duplicate entry based on productId and priceTagId. If we remove duplicates, we need to add the value
here in productDetails list the same productId is, but the quantity is different if I need to add the quantity to one
"productDetails" : [ { "productId" : "5764dfb7d991390e25edff74", "quantity" : 2, "netQty" : "10mg", "priceTagId" : 1, "alertAvailablity" : "Success" }, { "productId" : "5764dfb7d991390e25edff74", "quantity" : 4, "netQty" : "10mg", "priceTagId" : 1, "alertAvailablity" : "Success" }, { "productId" : "5764dfb7d991390e25edff74", "quantity" : 6, "netQty" : "30mg", "priceTagId" : 3, "alertAvailablity" : "Success" }, { "productId" : "5764dfb7d991390e25edff74", "quantity" : 8, "netQty" : "30mg", "priceTagId" : 3, "alertAvailablity" : "Success" }, { "productId" : "2345dfb7d991390e25edf659", "quantity" : 8, "netQty" : "30mg", "priceTagId" : 3, "alertAvailablity" : "Success" } ],
I got the final result as
"productDetails" : [ { "productId" : "5764dfb7d991390e25edff74", "quantity" : 6, "netQty" : "10mg", "priceTagId" : 1, "alertAvailablity" : "Success" }, { "productId" : "5764dfb7d991390e25edff74", "quantity" : 14, "netQty" : "30mg", "priceTagId" : 3, "alertAvailablity" : "Success" }, { "productId" : "2345dfb7d991390e25edf659", "quantity" : 8, "netQty" : "30mg", "priceTagId" : 3, "alertAvailablity" : "Success" } ],
Based on productId and priceTagId, I need to remove duplicates and add quantity from deleted duplicates record
private List<ProductDetail> removeDuplicateProducts(List<ProductDetail> productDetails) throws BaseException { for (ProductDetail eachProductDetail : productDetails) { for (ProductDetail eachInnerProductDetail : productDetails) { if(eachProductDetail.getProductId().equals(eachInnerProductDetail.getProductId())) { if(eachProductDetail.getPriceTagId().equals(eachInnerProductDetail.getPriceTagId())) { eachProductDetail.setQuantity(eachProductDetail.getQuantity()+eachInnerProductDetail.getQuantity()); productDetails.clear(); } } } } return productDetails; }
But I understand that this is so? What's wrong?