I have an ArrayList
that contains some object, such as User
, and each object has a name
and password
property. How can I
You are probably faced with the ConcurrentModificationException
while trying to remove object from the List
. An explanation for this exception is that the iterator of the ArrayList
is a fail-fast iterator. For example, it will throw an exception (fail) when it detects that its collection in the runtime has been modified. The solution to this problem is to use the Iterator
.
Here is a simple example that demonstrate how you could iterate through the List
and remove the element when specific condition is met:
List list = new ...
for (Iterator it = list.iterator(); it.hasNext(); ) {
User user = it.next();
if (user.getUserEmail().equals(currentUser.getUserEmail())) {
it.remove();
}
}