繁体   English   中英

如何在没有ConcurrentModificationException的情况下删除哈希图中的多个映射?

[英]How to remove multiple mappings in hashmap without ConcurrentModificationException?

我有2个哈希图: HashMap<String, Word> hmSmall, hmBig 每个中的映射都是多对一的 因此,基于某些标准,我必须删除两个哈希图中对应于特定值的所有映射。 为此,我正在关注这个答案。

问题是,在哈希表上进行迭代时,我仍然会收到ConcurrentModificationException

这是代码:

for (Iterator<Word> it = hmSmall.values().iterator(); it.hasNext(); i++) {
            Word value = it.next();//java.util.ConcurrentModificationException here
            String key = value.getKey();
            if (hmBig.containsKey(key)) {
                // if big contains less then remove from big
                // else it.remove

                if (hmBig.get(key).getTotalOccurances() < value
                        .getTotalOccurances()) {

                    hmBig.values().removeAll(
                            Collections.singleton(hmBig.get(key)));

                } else {

                    // it.remove(); not using this based on https://stackoverflow.com/a/30214164/848377
                    hmSmall.values().removeAll(Collections.singleton(value));

                }

            }
        }

有简单的出路吗?

Iterator.remove()必须通过Iterator.remove()删除,以避免ConcurrentModificationException

您的问题是,当您使用Iterator类遍历HashMap时,您正在通过删除元素来更改HashMap的大小...

您可以将要删除的对象存储在单独的集中,然后在循环之后删除所有对象。

比较下面这个stackoverflow帖子中的示例:

Map<String, String> map = new HashMap<>();
map.put("a", "");
map.put("b", "");
map.put("c", "");

Set<String> set = new HashSet<> ();
set.add("a");
set.add("b");

map.keySet().removeAll(set);

System.out.println(map); //only contains "c"

只需将removeAll调用放在循环外即可:

   public static void main(String[] args) {
    // The test map
    final Map<String, String> map = new HashMap<String, String>();
    map.put("Key1", "Value");
    map.put("Key2", "Value");
    map.put("Key3", "Value");

    .


    // Print the map to confirm it worked.
    for(final String key : map.keySet()) {
        map.values().removeAll(Collections.singleton("Value"));
       System.out.println(key + " = " + map.get(key));
    }
}

前一个会导致异常( ConcurrentModificationException ),请尝试以下操作:

 public static void main(String[] args) {
    // The test map
    final Map<String, String> map = new HashMap<String, String>();
    map.put("Key1", "Value");
    map.put("Key2", "Value");
    map.put("Key3", "Value");


    map.values().removeAll(Collections.singleton("Value"));


    for(final String key : map.keySet()) {
       System.out.println(key + " = " + map.get(key));
    }
}

现在工作正常

正如@Ejp所建议的那样, Iterator#remove()是摆脱ConcurrentModificationException的最佳解决方案。

此处类似的帖子遍历Collection,避免在循环中删除时避免ConcurrentModificationException

同样在java8中,您可以很好地使用removeIf()方法。 一遍遍Collection,避免在循环中删除时避免ConcurrentModificationException

因此,此处的重点是不要在迭代时更改哈希图的大小。 因此,我没有在循环中删除条目,而是在数组列表中添加了键。 当循环结束时,我遍历了该数组列表并删除了与该数组列表中的键相对应的哈希映射的条目。

这是代码:

ArrayList<String> alsmallhm=new ArrayList<String>();
        for (Iterator<Word> it = hmSmall.values().iterator(); it.hasNext(); i++) {
            Word value = it.next();//java.util.ConcurrentModificationException here
            String key = value.getKey();
            if (hmBig.containsKey(key)) {
                // if big contains less then remove from big
                // else it.remove

                if (hmBig.get(key).getTotalOccurances() < value
                        .getTotalOccurances()) {

                    hmBig.values().removeAll(
                            Collections.singleton(hmBig.get(key)));

                } else {

                    // it.remove();
                    alsmallhm.add(key);
                    /*hmSmall.values().removeAll(Collections.singleton(value));*/

                }

            }
        }

        for(String s:alsmallhm){
            hmSmall.values().removeAll(Collections.singleton(hmSmall.get(s)));
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM