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

Time:05-25

I have an arraylist that has multiple indexes and multiple elements within each index. I currently have a function that distincts the values in each element, however I would like to know how I can check each element to see if it exists in the other indexes of the arrayList and remove it.

So for exmaple each Serials index does not contain any duplicates but I would like to checkand remove "12505" from index 1 and 2 as it is being repeated.

ArrayList structure e.g

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;
}

CodePudding user response:

You can try the below:

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;
}

CodePudding user response:

A stream based approach which doesn't care if the data is continuous:

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 for reference:

class ResourcesList {
    Object category;
    String serials;

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

    }
}
  • Related