3

I am trying to program an IM software, I want to let user leave the conversation and tell his partner that he has left... I prefer to use for loop instead Iterator, seek all the users and get the user who ask to leave and remove him... like that:

   for(Clientuser Cu: EIQserver.OnlineusersList)
          if(Cu.ID.equals(thsisUser.ID)) // find the user who ask to leave 
          {
          Omsg.setBody("@@!&$$$$@@@####$$$$"); //code means : clien! ur parter leaves...
                 sendMessage(Omsg); // sed message to thje partner with that code
                 EIQserver.OnlineusersList.remove(Cu);// remove the partner
                EIQserver.COUNTER--;// decrease counter.

          }

I get Exception: java.util.ConcurrentModificationException

I was using iterators, and to get rid of this exception, I convert to for, but the same exception still appears!! how may I get rid of this exception?

6
  • 3
    You cannot remove something from a list which you are looping through.
    – Trick
    Oct 31, 2013 at 9:20
  • 1
    @Trick unless you remove it using the same iterator you are using on the loop or you are using a collection that supports it, like CopyOnWriteArrayList.
    – aalku
    Oct 31, 2013 at 9:24
  • You should definitely read this (not an answer): oracle.com/technetwork/java/codeconventions-135099.html#367
    – aalku
    Oct 31, 2013 at 9:28
  • True. By the way @EsmaeelQash, try to follow Java Coding Standards (Naming Conventions). If you will ever pass your code, it create a pain for your follower :)
    – Trick
    Oct 31, 2013 at 9:28
  • I'll edited the question to follow naming conventions...
    – aalku
    Oct 31, 2013 at 9:29

5 Answers 5

7

Use Iterator instead of looping. For example:

Iterator<Clientuser> iterator = EIQserver.OnlineusersList.iterator();
while (iterator.hasNext()) {
    Clientuser next = iterator.next();
    if(next.ID.equals(thsisUser.ID)) {
        Omsg.setBody("@@!&$$$$@@@####$$$$"); 
        sendMessage(Omsg); 
        iterator.remove();// remove the partner
    }
}
2
  • thank you, kocko, but does iterator.remove() removes the object from the EIQserver.OnlineusersList?? Oct 31, 2013 at 10:06
  • Yes, it removes the last fetched object from list. Oct 31, 2013 at 10:09
3

Faulting line: EIQserver.OnlineusersList.remove(Cu);

You can only remove elements from a collection that is being iterated over via the Iterator object you are using to iterate.

for (Iterator<Clientuser> it = EIQserver.OnlineusersList.iterator(); it.hasNext();)
{
    Clientuser cu = it.next();
    if (!cu.ID.equals(thsisUser.ID))
        continue;
    // other code
    it.remove();
}
1

Use Iterator for do something with list in loop:

Iterator<Clientuser> iter = EIQserver.OnlineuserList.iterator();
for(;iter.hasNext();) {
    Clientuser Cu = iterator.next();
    if(Cu.ID.equals(thsisUser.ID)) {
        Omsg.setBody("@@!&$$$$@@@####$$$$"); 
        sendMessage(Omsg); 
        iterator.remove(next);
    }
}
0

One possible solution is also to transform the Collection to HashMap, save id's to remove and then remove it from the HashMap.

Collection<Integer> removeIds = new ArrayList<Integer>();
Map<Integer,ClientUser> all = new HashMap<Integer,ClientUser>();

for(Clientuser Cu: EIQserver.OnlineusersList) {
all.put(cu.ID,Cu);
      if(Cu.ID.equals(thsisUser.ID)) // find the user who ask to leave 
      {
      Omsg.setBody("@@!&$$$$@@@####$$$$"); //code means : clien! ur parter leaves...
             sendMessage(Omsg); // sed message to thje partner with that code
            EIQserver.COUNTER--;// decrease counter.
    removeIds.add(Cu.ID);

      }
}
-2

As you are Iterating the EIQserver collection class, you can not remove element from the same class. Use a different collection for Iteration and remove the element from EIQserver class.

 List temp = ListofEIQserverobject;
  for(Clientuser Cu: temp.OnlineusersList){
    ..... your code then 
      EIQserver.OnlineusersList.remove(Cu);
    }
1
  • To do that temp should be a copy of the list, not a reference to itself.
    – aalku
    Oct 31, 2013 at 9:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.