遍历集合时不要修改集合的理解
public static void main(String[] args) {//创建一个集合Collection books = new HashSet();books.add("One book");books.add("Two book");books.add("Three book");//获取books集合对应的迭代器Iterator it = books.iterator();try{while(it.hasNext()){String book = (String)it.next();System.out.println(book);//note: 如果删除"Two book",本示例不会引发异常if (book.equals("Three book")){//使用Iterator迭代过程中,不可修改集合元素!books.remove(book);}}System.out.println("移除元素之后:"+books);}catch (ConcurrentModificationException e) {System.out.println(e);}}
if (modCount != expectedModCount) throw new ConcurrentModificationException();
if (book.equals("Three book")){//使用Iterator迭代过程中,通过它来移除集合元素是安全的it.remove();}
public void remove() { if (current == null) throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); Object k = current.key; current = null; HashMap.this.removeEntryForKey(k); expectedModCount = modCount; }