1

I am trying to remove an object with a certain attribute from a list of Objects, I have red other questions about this problem but I guess I am missing something.

this method doesn't work, because I guess you can not remove elements from a list while iterating through it, so I created a Dog removeDog = null; variable, which is set to the right dog during the loop, and after it finished to iterate I can just do animals.remove(removeDog); (I don't know if this is the best solution)

for (Dog dog : animals.getDogList()){
  if (dog.getDogName().equals(dogName)){
    animals.getDogList().remove(dog);
  }
}

This method instead works perfectly, in my eyes it should have the same problem of the previous one, but in this case Java let me remove one or more elements during iteration, but I don't understand the difference.

for (Dog dog : animals.getDogList()){
  if (dog.getWeigh() > weight){
    animals.getDogList().remove(dog);
  }
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
jaymartines
  • 35
  • 1
  • 4
  • You should use iterator instead. Iterator allows remove during loop – Maxim Tulupov Jan 30 '19 at 18:43
  • Your last example can also fail, if you really think it is different, then please provide a [mcve] that demonstrates the difference. Likely none of the objects match the condition, or maybe only the last entry in the list is removed (which is an edge case which won't trigger the ConcurrentModificationException).. – Mark Rotteveel Jan 30 '19 at 19:03
  • indeed you were right, I tried to ask more complex operations to that loop and it gives same error as the first loop, solved with iterator – jaymartines Jan 31 '19 at 01:26

3 Answers3

0

Use an iterator to remove an element while iterating through a List.

In your method you are first iterating through the loop to identify a match and store the match in a temporary variable called removeDog. This method of yours has a limitation that it can remove only dog that matches the given condition.

for (Iterator<Dog> iterator = animals.getDogList().iterator(); iterator.hasNext();) {
    Dog dog= iterator.next();
    if (dog.getDogName().equals(dogName)){
        // Remove the current dog from the iterator and the list.
        iterator.remove();
    }
}

This method is better (and safer) than yours. If you use my method, you can remove all the dogs that match the given condition.

If you like succinct code, you can also use Collections::removeIf method. See the following one-liner that uses lambda. It will work with Java 8 or later.

animals.getDogList().removeIf(dog -> dog.getDogName().equals(dogName));
VHS
  • 9,534
  • 3
  • 19
  • 43
0

Iterator doggerator = animals.getDogList().iterator(); while(doggerator.hasnext(){ ... doggerator.remove() ... }

gcpdev-guy
  • 460
  • 1
  • 10
  • 23
-1

Class java.util.Iterator has a remove() method to allow you to remove an item from a list while iterating through the list. The below URL has examples and more explanation.

https://www.geeksforgeeks.org/remove-element-arraylist-java/

Abra
  • 19,142
  • 7
  • 29
  • 41