I have a remove method in my customList class that removes a an object passed as a parameter from from my customList object where contentInCustomList.equals(parameter) and returns a boolean indicating if anything was removed.
I am trying to implement a remove all method that takes a collection, performs the remove method for each member of the collection passing it as a parameter and returns a boolean indicating if anything was removed.
To do this, I initialized a boolean (toReturn) to false and made it so that, for each member of the collection, I'd change it (toReturn) to true if remove(i) or the boolean are true. This way, toReturn would stay true if it's been changed to true.
public boolean removeAll(@NotNull Collection c) {
boolean toReturn = false;
c.forEach(i -> toReturn = remove(i) || toReturn);
return toReturn;
}
However, when I try doing this, it gives me an error and intellliJ tells me to convert it to an AtomicBoolean:
public boolean removeAll(@NotNull Collection c) {
AtomicBoolean toReturn = new AtomicBoolean(false);
c.forEach(i -> toReturn.set(toReturn.get() || remove(i)));
return toReturn.get();
}
Why does the first not work but the second does?