Why the ConcurrentModificationException?

Hello my friends from Java.

When you run the following code:
143        // Create the Clique's medium.
144        this.medium.addAll(ownerIdentity.getConnection().getMediaProfileNode().flattenMediaProfile());
145        for(MI i:candidateIdentities){
146            Set<SourceMediaProfile>memberMedium=i.getConnection().getMediaProfileNode().flattenMediaProfile();
147
148            for(SourceMediaProfile ownerMediumMediaProfile:this.medium){
149                if(memberMedium.contains(ownerMediumMediaProfile)){
150                    continue;
151                }
152                this.medium.remove(ownerMediumMediaProfile);
153            }
...
165        }
I get the following excetption:
java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810)
        at java.util.HashMap$KeyIterator.next(HashMap.java:845)
        at cliquespace.core.agentdevice.source.SourceClique.<init>(SourceClique.java:148)
The flattenMediaProfile method has the following code:
114    public Set<SourceMediaProfile>flattenMediaProfile() {
115        Set<SourceMediaProfile>mps=new HashSet<SourceMediaProfile>();
116        mps.add(this);
117
118        MPN mpn=null;
119        try{
120            mpn=(MPN)this;
121            Collection<SourceMediaProfile<CS,?extends MediaProfileIdentifier,MPN,C,I,MI,P,OP,CL>>mpc=mpn.getParentMediaProfiles();
122            for(SourceMediaProfile p:mpc){
123                mps.addAll(p.flattenMediaProfile());
124            }
125        }catch(ClassCastException cce){/*Do nothing.*/}
126
127        return mps;
128    }
Now, I can probably create tables to do what I'm trying to do with these collections, but I think that I could do something wrong with the collectibles, and, rather than to remain ignorant, I would like to take this opportunity to discover what it might be.

Your considered advice would be welcomed.

Thank you

Owen.

It is simply because you can't delete an item in a collection or list, while in a foreach loop over that.
Instead of

  for(SourceMediaProfile ownerMediumMediaProfile:this.medium)

Use an iterator as follows:

Iterator iterator = this.medium.iterator() ;
while (iterator.hasNext()){
SourceMediaProfile ownerMediumMediaProfile = iterator.next() ;
// do the remove as you wish.
}

Tags: Java

Similar Questions

Maybe you are looking for

  • How can I get the return status of the page load indicator?

    After updating to Firefox 29.0.1, the little spinner loading status page has disappeared.There is more to the display/bar Tools/Customize screen, so I can't put it in the menu barI was doing. Is there an add-on for this?

  • How to split the screen of my mac book

    How to split the screen of my Macbook?

  • Whole singed to hexadecimal string to number converter

    HI guys... I want to convert the number to hex string to number converter but when I connect integer, otherwise it does not change the value (as if you write 'EC78' you should get "-5000", but I get the value in the range of 60 thousand ")... Any ide

  • the usurpation of EAPOL-logoff

    Hello 802 plaintiffs. 1 x Microsoft do not send an EAPOL logoff because Microsoft has no confidence in the log packets. These packages can not be authenticated and can be easily spoofed. Microsoft indicates that an authenticator of 802. 1 x should no

  • License on multiple machines

    Hi all!I'm new here and I am writing here because there are no resellers that allows in Italy has.I need to upgrade my co-worker Suites, but I do not understand what to buy.For the moment, we have an iMac with suite CS6 and a MacBook pro with another