ConcurrentModificationException and a HashMap

Iterator it = map.entrySet().iterator();while(it.hasNext()){Entry item = it.next();
   map.remove(item.getKey());}

这种方法会出现错误

 

正确的删除办法是

Iterator it = map.entrySet().iterator();

   while (it.hasNext())

   {

      Entry item = it.next();

      it.remove();

   }

 

 

 

你可能感兴趣的:(java,Java)