【发布时间】:2012-12-28 18:14:20
【问题描述】:
我有一个将字节映射到字节集的映射。我想浏览地图并对场景进行更改。
private HashMap<Byte, HashSet<Byte>> table;
...
Iterator<Entry<Byte, HashSet<Byte>>> it = table.entrySet().iterator();
while( it.hasNext() ) {
Map.Entry<Byte, HashSet<Byte>> pairs = it.next();
byte node = pairs.getKey();
HashSet<Byte> hSet = pairs.getValue();
Iterator<Byte> setIter = hSet.iterator();
while( setIter.hasNext() ) {
byte sNode = setIter.next(); // Throws a ConcurrentModificationException
...
}
}
当我尝试遍历子迭代器时,此代码会引发 ConcurrentModificationException。我应该怎么做才能在我的地图中迭代和更改这个集合?
【问题讨论】:
-
你想做出什么样的改变?
-
使用Guava
HashMultimap可能会更好。 -
尝试模拟您的问题。但是代码对我来说工作得很好。您能发布任何其他详细信息吗?
标签: java collections hashmap hashset