Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Java-迭代Hashmap?_Java_Hashmap - Fatal编程技术网

Java-迭代Hashmap?

Java-迭代Hashmap?,java,hashmap,Java,Hashmap,你知道为什么会这样吗?发生这种情况时,映射中有一个键(带一个值)。我猜您的getRecipients()返回的集合与插件相同。在查看中 这意味着您试图在迭代集合时从集合中删除元素。这当然不好 相反,你应该这样做 at java.util.HashMap$HashIterator.nextEntry(Unknown Source) at java.util.HashMap$EntryIterator.next(Unknown Source) at java.util.HashMap$EntryIt

你知道为什么会这样吗?发生这种情况时,映射中有一个键(带一个值)。

我猜您的
getRecipients()
返回的集合与
插件相同。在查看中

这意味着您试图在迭代集合时从集合中删除元素。这当然不好

相反,你应该这样做

at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
at java.util.HashMap$EntryIterator.next(Unknown Source)
at java.util.HashMap$EntryIterator.next(Unknown Source)
Vector toRemove=new Vector();
迭代器迭代器=plugin.inreview.keySet().Iterator();
while(iterator.hasNext()){
Player key=(Player)迭代器.next();
删除。添加(键);
}
chat.getRecipients().removeAll(toRemove);

另一种可能是您有多个线程?

chat.getRecipients
和plugin.inreview之间的关系是什么?@JonSkeet没有关系。这是给我的
chat.getRecipients
返回将被发送消息的玩家列表
this.plugin.inreview
是一个带有键和值的Hashmap。如果您注释掉
chat.getRecipients()。删除(键)仅用于测试和运行,您是否仍然会收到此错误。我认为这是一种关系,您在迭代时从同一个集合中删除元素。我同意,但更喜欢LinkedList而不是Vector。不过,我的偏好并没有充分的理由。
getRecipients()
plugin.inreview
不同。讽刺的是,这个相同的代码位被另一个.Java文件使用,并且工作得非常好。两者的设置方式相同。。。
at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
at java.util.HashMap$EntryIterator.next(Unknown Source)
at java.util.HashMap$EntryIterator.next(Unknown Source)
Vector toRemove=new Vector();
Iterator<Player> iterator = plugin.inreview.keySet().iterator();
while (iterator.hasNext()) {
  Player key = (Player) iterator.next();
  toRemove.add(key);
}
chat.getRecipients().removeAll(toRemove);