0

I am trying to filter out a list based on values. I have two List. One is a list of names which i want to remove i.e present in animalList. And another is the main primary list AnimalPrimaryDataPojoFilterList from where i have to remove the object which matches the names from animalList. Now i do have the solution but i think it takes lot of time. Below is the code. I am using Java 8. Can it be optimised?

if(animalList!=null && animalList.size()>0)
        {
            for(AnimalFilterPojo dtoObject:animalList)
            {
                if(!dtoObject.getApproved())
                {
                    for(AnimalPrimaryDataPojo mainDtoObject: AnimalPrimaryDataPojoFilterList)
                    {
                        if(mainDtoObject.getAnimalName().equalsIgnoreCase(dtoObject.getValue()))
                        {   
                            AnimalPrimaryDataPojoFilterList.remove(mainDtoObject);
                        }
                    }
                }
            }
2
  • 1
    If you want to remove elements from a list while iterating on it, you will have to manually instantiate the iterator and use iterator.remove() or you will have this problem. A better solution is to use stream(), filter() and collect(Collectors.toList()). Commented Nov 10, 2017 at 11:12
  • Optimized for what? Speed? Amount of braces? This piece of code is probably an indication of a larger design problem, so even if you convert this to use streams it won't really help. Your naming is also quite clumsy, adding Pojo to the end of a class name, especially when you then use dtoObject as a variable name. Commented Nov 10, 2017 at 11:55

4 Answers 4

2

Use removeAll() method.

AnimalPrimaryDataPojoFilterList.removeAll(animalList);

It will remove the objects of animalList from AnimalPrimaryDataPojoFilterList

N.B: You need to implement hashCode() and equals() method in AnimalFilterPojo

Sign up to request clarification or add additional context in comments.

2 Comments

Cannot use. Both list are different. AnimalPrimaryDataPojoFilterList has one of the property of animalList
@sTg Then how are you writing AnimalPrimaryDataPojoFilterList.remove(dtoObject); ?? it should be AnimalPrimaryDataPojoFilterList.remove(mainDtoObject);
1

You can use Java 8 streams to filter the list. In below example Parent is the object which has abc property of type String. We are filtering List<Parent> objs using List<String> names

public class ListFilterDemo {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        List<Parent> objs = new ArrayList<>();

        List<Parent> filtersObjs = objs.parallelStream().filter((obj) -> names.contains(obj.getAbc())).collect(Collectors.toList());
    }
}

class Parent {
    private String abc;

    public Parent(String abc) {
        this.abc = abc;
    }

    public String getAbc() {
        return this.abc;
    }
}

Comments

1

You can try this:

if(animalList!=null && animalList.size()>0)
    animalList.removeIf(animal ->
        AnimalPrimaryDataPojoFilterList.stream()
           .filter(filter -> !filter.getApproved())
           .map(AnimalFilter::getValue)
           .collect(Collectors.toList()).contains(animal.getAnimalName()));

to explain the code: here we use removeIf() on the List to remove the objects using a Predicate that is a lambda that receives the animal and filters the list by removing the elements by name where name is taken from a list generated as a selection of the AnimalPrimaryDataPojoFilterList of the elments that have the approved flag (the second filter), extracting the value (using the map) and constructing a list out of it using a Collector.

The portion:

AnimalPrimaryDataPojoFilterList.stream()
   .filter(filter -> !filter.getApproved())
   .map(AnimalFilter::getValue)
   .collect(Collectors.toList())

generates the list to be used as a filter

animalList.removeIf(animal ->
    <generated list>.contains(animal.getAnimalName()));

uses list generated in place to apply the filter. Beware that this of course modifies the list you have Besides, you should not start a variable with a capital letter like you did for AnimalPrimaryDataPojoFilterList.

Comments

0

you can use removeIf then use AnimalPrimaryDataPojoFilterList as the source in which case you'll need to invert your logic within the if block i.e:

if(animalList != null && animalList.size() > 0){
      AnimalPrimaryDataPojoFilterList.removeIf(x -> 
                    animalList.parallelStream()
                   .filter(e -> !e.getApproved() && x.getAnimalName().equalsIgnoreCase(e.getValue())).findAny().orElse(null) != null);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.