Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Collections 迭代器如何从集合中同时读取和删除元素?_Collections_Linked List_Hashmap_Thread Safety_Concurrenthashmap - Fatal编程技术网

Collections 迭代器如何从集合中同时读取和删除元素?

Collections 迭代器如何从集合中同时读取和删除元素?,collections,linked-list,hashmap,thread-safety,concurrenthashmap,Collections,Linked List,Hashmap,Thread Safety,Concurrenthashmap,我创建了以下代码。 我的目标是检查在读取集合时是否使用迭代器删除元素。 有人能解释一下为什么在将hashmap的值集合添加到链接列表中并为该列表创建迭代器后会抛出concurrentModificationException,但在将集合添加到列表中获得迭代器时不会抛出concurrentModificationException 我知道原因很简单,我想买些容易买到的东西,但我只想确认一下我的想法是否正确 还有另外两点。 1.因为hashmap不是线程安全的,所以为什么我们不能在迭代器读取时向它添

我创建了以下代码。 我的目标是检查在读取集合时是否使用迭代器删除元素。 有人能解释一下为什么在将hashmap的值集合添加到链接列表中并为该列表创建迭代器后会抛出concurrentModificationException,但在将集合添加到列表中获得迭代器时不会抛出concurrentModificationException

我知道原因很简单,我想买些容易买到的东西,但我只想确认一下我的想法是否正确

还有另外两点。 1.因为hashmap不是线程安全的,所以为什么我们不能在迭代器读取时向它添加任何元素? 2.如果是,那么我们如何从地图中删除元素

 package com.Users;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

    public class Temp{
        public static void main(String arg[]){
            HashMap<String, String> map=new HashMap();
            map.put("A", "B");
            map.put("C", "D");

            LinkedList<String> list=new LinkedList<>();

            //****** concurent exception is thrown when ever line 1 is swapped with line 2 ******
            Iterator<String> iterator=list.iterator();  //line no. 1
            list.addAll(map.values());    //line no. 2
            //******************

            while(iterator.hasNext()){
                if(iterator.next().equals("B")){
                    iterator.remove();
                }
            }


            System.out.println(list);
        }
    }
package.com.Users;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.LinkedList;
公共类临时工{
公共静态void main(字符串arg[]){
HashMap=newHashMap();
地图放置(“A”、“B”);
地图放置(“C”、“D”);
LinkedList=新建LinkedList();
//******当第1行与第2行交换时,将引发concurent异常******
迭代器迭代器=list.Iterator();//第1行
list.addAll(map.values());//第2行
//******************
while(iterator.hasNext()){
if(iterator.next().equals(“B”)){
iterator.remove();
}
}
系统输出打印项次(列表);
}
}

HashMap上的迭代器本质上是
故障快速的
,这意味着它们会尽快中止操作,并立即暴露故障

集合维护一个名为
modCount
的内部计数器。无论何时从集合中添加或删除项,都会修改此计数器

迭代时,每次调用next()时,都会将modCount的当前值与初始值进行比较。如果存在不匹配,它将抛出
ConcurrentModificationException
,从而中止整个操作


  • 由于hashmap不是线程安全的,这就是为什么在迭代器读取时我们不能向它添加任何元素的原因吗
  • 我们无法修改集合,因为我们正在对其进行迭代,因为大多数集合(如ArrayList、HashMap)在默认情况下都有fail-fast迭代器


  • 如果是,那么我们如何从地图中删除元素
  • 在您的示例中,我们可以删除元素,因为我们在
    iterator.remove()
    中使用了迭代器的
    remove()
    方法。 如果我们使用集合的
    remove()
    方法,那么也会抛出
    ConcurrentModificationException


    阅读详细解释。

    有道理!!谢谢你的解释。