我希望根据productId和priceTagId删除重复的条目。如果我们删除了重复项,需要添加数量。
在productDetails列表中,相同的productId存在但数量不同,如果需要将数量添加到一个中
"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"
}
],
我得到的最终输出是
"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"
}
],
基于productId和priceTagId,我需要删除重复项并在删除的重复项中添加数量。
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;
}
但我不明白为什么?出了什么问题吗?