I have objects of a class Choice which are in a list.
Choice looks like this.
public class Choice extends MorphiaModel{
public String name;
public Double price;
}
Some of them are empty, that is name is “” and price is null.
I want to remove these empty values.
I tried iterating over the list and removing them empty Choice objects, but I got a ConcurrentModificationException then I did this (after implementing equals and hashcode), but it doesn’t work, the empty values are still there.
Note: option.choices is a list of Choice objects
Choice emptyChoice = new Choice();
emptyChoice.name = "";
emptyChoice.price = null;
option.choices.remove(emptyChoice);
The
ConcurrentModificationExceptionhappened because you tried to remove the elements outside of theIteratoriterating over the list.Something like this works: