我有一个循环来哈希表。如果满足条件,则需要从哈希图中删除键值对。我无法使用以下代码来做到这一点。你能帮我实现这个吗?
for(HashMap.Entry<Integer,Character> m:commons.entrySet()){
while(i!=(int)m.getKey()){
i++;
}
if(s2.charAt(i)!=(int)m.getKey()){
commons.remove((int)m.getKey());
}
}
You can use an
Iterator
to safely remove while iterating over aMap
.You can also look at
.removeIf()
(Java 8+), but with a loop inside your iteration, this is more readable, imo.