-7
public void execute(HashMap<String,Coordonnee >c)
{
    c.forEach((k,v) -> {
        p = m.getMin(c);
        sky.add(p);
        c.remove(p.getNom());
    });
}

This throws a java.util.ConcurrentModificationException.

How can I fix that ?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
user9467051
  • 118
  • 2
  • 11

2 Answers2

0
  1. Use removeIf function: map.entrySet().removeIf
  2. Use iterator:

    Iterator<Object> it = map.keySet().iterator();
    
    while (it.hasNext())
    {
       it.next();
       if (something)
           it.remove();
    }
    
Guts
  • 748
  • 4
  • 10
0

you cannot delete an element of the list while looping through it. You should use Iterator for that.

public void execute(HashMap<String,Coordonnee >c)
{
    Iterator cItr = c.iterator();
    while(cItr.hasNext())
    {
        c = cItr.next();
        p = m.getMin(c);
        sky.add(p);
        cItr.remove(p.getNom());
    }
}
Hassan Mudassir
  • 191
  • 2
  • 10