如何比较 ArrayList 的元素并删除它(如果它存在于前一个元素中)- Java

How to compare elements of an ArrayList and remove it if it exists in the previous element - Java

我有一个数组列表,它有多个索引和每个索引中的多个元素。我目前有一个函数可以区分每个元素中的值,但是我想知道如何检查每个元素以查看它是否存在于 arrayList 的其他索引中并将其删除。

例如,每个 Serials 索引不包含任何重复项,但我想检查并从索引 1 和 2 中删除“12505”,因为它正在重复。

A​​rrayList 结构例如

ResourcesList = ArrayList<E>
[0]ResourcesList
-Category = null
-Serials  = "25168,36587,12505,14568,"
[1]ResourcesList
-Category = null
-Serials  = "20514,98610,368970,12505,"
[2]ResourcesList
-Category = null
-Serials  = "689105,85015,12505,68970," ```


private List<ResourcesList > removeDuplicateSerials(List<ResourcesList > resourceslist ){
    
    ArrayList<ResourcesList > noduplicates = new ArrayList<ResourcesList >();

    
    for(ResourcesList resourceslist : resourceslist ){
        List<String> serials = Arrays.asList(resourceslist.getSerial().split("\s*,\s*")).stream() 
                .distinct() 
                .collect(Collectors.toList());
        resourceslist .setCount(serials.size());
        resourceslist .Setserials(String.join(",", serials));
        noduplicates.add(resourceslist );
    }
    
    
    
    return noduplicates;
}

您可以尝试以下方法:

private static List<ResourcesList> removeDuplicateSerials(List<ResourcesList> resourcesList) {

    ArrayList<ResourcesList> noduplicates = new ArrayList<ResourcesList>();
    List<String> mListString = new ArrayList<>();
    for (int i = 0; i < resourcesList.size(); i++) {
        List<String> serials = Arrays.asList(resourcesList.get(i).getSerials().split("\s*,\s*")).stream()
                .distinct()
                .collect(Collectors.toList());
        ResourcesList resourceslist = new ResourcesList();
        if (i == 0) {
            resourceslist.setCount(serials.size());
            resourceslist.setSerials(String.join(",", serials));
            noduplicates.add(resourceslist);
            mListString.addAll(serials);
        } else {
            serials.removeAll(mListString);
            resourceslist.setCount(serials.size());
            resourceslist.setSerials(String.join(",", serials));
            noduplicates.add(resourceslist);
        }
    }

    return noduplicates;
}

不关心数据是否连续的基于流的方法:

public static void main(String[] args) {
    List<ResourcesList> resourceList = new ArrayList<ResourcesList>();
    resourceList.add(new ResourcesList(null, "25168,36587,12505,14568,"));
    resourceList.add(new ResourcesList(null, "20514,98610,12505,368970,"));
    resourceList.add(new ResourcesList(null, "689105,85015,12505,68970,"));

    List<String> data = new ArrayList<>();
    Collections.reverse(resourceList); // reverse list to start from behind
    List<ResourcesList> collect = resourceList.stream()
            .collect(ArrayList<ResourcesList>::new, (x, y) -> {
                y.serials = Stream.of(y.serials.split(","))
                        .filter(e -> e != null && !data.contains(e))
                        .collect(Collectors.joining(",")); //remove items we already encountered
                x.add(y);
                data.addAll(Arrays.asList(y.serials.split(","))); //save items we already encountered
            }, (x, y) -> x.addAll(y));
    Collections.reverse(collect); //reverse back to original order
    System.out.println(collect);
}

ResourcesList class 供参考:

class ResourcesList {
    Object category;
    String serials;

    public ResourcesList(Object category, String serials) {
        this.category = category;
        this.serials = serials;

    }
}