如何根据两个自定义对象删除列表中的重复项

时间:2016-06-14 13:22:18

标签: java sorting collections duplicates

我有ProductDetail类,其中包含两个变量productNameproductVersion。我需要根据最新的productVersion替换列表中的重复项目。

示例:

class ProductDetails {
   string productName;
   string productVersion;
}
new ProductDetails("ios", "9.1.0")
new ProductDetails("android", "6.0.1")
new ProductDetails("android", "5.1.1")
new ProductDetails("ios", "10.0.0")

结果: android 6.0.1 ios 10.0.0

提前致谢。

2 个答案:

答案 0 :(得分:2)

您可以将所有这些对象放入Map<String, ProductDetails>并保留最新版本的对象。

List<ProductDetails> details = Arrays.asList(
        new ProductDetails("ios", "9.1.0"),
        new ProductDetails("android", "6.0.1"),
        new ProductDetails("android", "5.1.1"),
        new ProductDetails("ios", "10.0.0"));

// get part of version string
Function<Integer, Function<ProductDetails, Integer>> version = 
        n -> (pd -> Integer.valueOf(pd.getProductVersion().split("\\.")[n]));
// chain to comparator
Comparator<ProductDetails> versionComp = Comparator.comparing(version.apply(0))
        .thenComparing(version.apply(1)).thenComparing(version.apply(2));

Map<String, ProductDetails> latest = new HashMap<>();
for (ProductDetails pd : details) {
    String name = pd.getProductName();
    if (! latest.containsKey(name) || versionComp.compare(latest.get(name), pd) < 0) {
        latest.put(name, pd);
    }
}

之后,latest是:

{android=Sandbox.ProductDetails(productName=android, productVersion=6.0.1), 
 ios=Sandbox.ProductDetails(productName=ios, productVersion=10.0.0)}

或者,您可以使用Collectors.groupingBy,然后使用相同的Comparator

details.stream()
        .collect(Collectors.groupingBy(ProductDetails::getProductName))
        .values().stream().map(list -> Collections.max(list, versionComp))
        .forEach(System.out::println);

答案 1 :(得分:2)

这应该适合你:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


public class ProductDetails {

private String productName;
private String productVersion;
private List<ProductDetails> productData = null;

public ProductDetails(String productName,String productVersion){
    this.productName = productName;
    this.productVersion = productVersion;
    if(productData == null) {
        productData = new ArrayList<ProductDetails>();
        adNewProduct(this);
    }
}

private void  adNewProduct(ProductDetails p){
    String[] s1 = p.productVersion.split(Pattern.quote("."));
    for (ProductDetails dpp: productData) {
        if (dpp.productName.equals(p.productName)) {
            int index = productData.indexOf(dpp);
            String[] s2 = dpp.productVersion.split(Pattern.quote("."));
            for(int i = 0; i < s1.length; i++) {
                int v1 = Integer.valueOf(s1[i]);
                int v2 = Integer.valueOf(s2[i]);

                if (v1 > v2) {
                    productData.set(index,p);
                    return;
                }
            }
        }
    }
    productData.add(p);
}


@Override  // you can modify it to how you want
public String toString(){

    String s = "";

    for (ProductDetails p: productData){
        s += "ProductName: " + p.productName + " ProductVersion: " + p.productVersion + "\n";
    }
    return s;
}


//the main method
public static void main(String[] args) {
    ProductDetails k = new ProductDetails("ios", "9.1.1");
    k.adNewProduct(new ProductDetails("android", "5.1.1"));
    k.adNewProduct(new ProductDetails("android", "6.0.1"));
    k.adNewProduct(new ProductDetails("ios", "10.0.0"));

    System.out.println(k);

    }

}

输出:

  

产品名称:ios ProductVersion:10.0.0

     

ProductName:android ProductVersion:6.0.1