gpt4 book ai didi

java - 如何从java中的ArrayList中删除重复条目

转载 作者:搜寻专家 更新时间:2023-10-31 20:09:31 25 4
gpt4 key购买 nike

我想删除基于 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;
}

但我不明白吗?怎么了?

最佳答案

最有效的解决方案是使用 Map,其中键是您认为使产品相同的所有字段的组合,值包含任何附加信息。

在你的情况下你可以做

private Collection<ProductDetail> accumulateDuplicateProducts(List<ProductDetail> productDetails) {
// use a map to quickly find entries which match.
// using a linked HashMap means the order of addition is preserved.
Map<String, ProductDetail> productMap = new LinkedHashMap<>();
for (ProductDetail pd : productDetails) {
// build a composite key of the fields you want to match on.
String key = pd.getProductId() + " " + pd.getPriceTag();
// if the Strings match they should be merged.
// if there was no previous entry, use the current one.
// if there was a previous entry call merge() to combine them.
productMap.compute(key, (k, pd2) -> pd2 == null ? pd : merge(pd, pd2));
}
return productMap.values();
}

private static ProductDetail merge(ProductDetail pd, ProductDetail pd2) {
// combine two ProductDetails
}

注意:如果使用两个嵌套循环,时间复杂度是 O(n) 而不是 O(n^2)

But i dint get it wy? What wrong?

你遇到的一个问题是

productDetails.clear();

您遇到的另一个问题是您将每个条目与每个条目进行比较,例如假设您有两个条目 AB 匹配

A is compared with A so A *= 2
A is compared with B do A += B
B is compared with A so B += A
B is compared with B so B *= 2

您最终还是得到了两个条目,因为您没有删除一个条目。

关于java - 如何从java中的ArrayList中删除重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38182119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com