Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.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_Iteration_Remove If - Fatal编程技术网

Java 迭代时删除hashmap中的条目

Java 迭代时删除hashmap中的条目,java,hashmap,iteration,remove-if,Java,Hashmap,Iteration,Remove If,我有一个循环来迭代hashmap。如果条件满足,我需要从hashmap中删除键、值对。我无法使用下面的代码执行此操作。我怎样才能做到这一点 for(HashMap.Entry<Integer,Character> m:commons.entrySet()){ while(i!=(int)m.getKey()){ i++; } if(s2.charAt(i)!=(int)m.getKey()){ commons.remove((int

我有一个循环来迭代hashmap。如果条件满足,我需要从hashmap中删除键、值对。我无法使用下面的代码执行此操作。我怎样才能做到这一点

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());
    }
}
for(HashMap.Entry m:commons.entrySet()){
而(i!=(int)m.getKey(){
i++;
}
if(s2.charAt(i)!=(int)m.getKey()){
remove((int)m.getKey());
}
}
for(迭代器it=map.entrySet().Iterator();it.hasNext();){
Map.Entry=it.next();
//转换为int在代码中是多余的。我删除了它。
而(i!=entry.getKey())i++;
if(s2.charAt(i)!=entry.getKey()){
it.remove();
}
}
在迭代
映射时,可以使用安全删除

您也可以查看(Java 8+),但在迭代中有一个循环,我认为这更可读。

for(Iterator it=map.entrySet().Iterator();it.hasNext();){
Map.Entry=it.next();
//转换为int在代码中是多余的。我删除了它。
而(i!=entry.getKey())i++;
if(s2.charAt(i)!=entry.getKey()){
it.remove();
}
}
在迭代
映射时,可以使用安全删除

您还可以查看(Java8+),但在迭代中有一个循环,这更具可读性

  • 您尝试这样做的方式可能会导致
    ConcurrentModificationException
    。您应该使用
    迭代器
    ,或者内部使用
    迭代器
  • 虽然可以将
    char
    int
    进行比较,但在比较中可能混淆了键和值。你已经写信了

    if(s2.charAt(i)!=(int)m.getKey())
    
    其中您正在将
    s2.charAt(i)
    整数的键进行比较。从语法上讲,这是正确的,但您可能希望比较值(这是一个
    字符
    ),也就是说,您很可能希望这样做

    if(s2.charAt(i)!=(int)m.getValue())
    
  • 按如下方式操作:

    for (Iterator<Entry<Integer, Character>> itr = commons.entrySet().iterator(); itr.hasNext();) {
        Entry<Integer, Character> entry = itr.next();
    
        while (i != entry.getKey()) {
            i++;
        }
    
        if (s2.charAt(i) != entry.getValue()) {
            itr.remove();
        }
    }
    
    for(迭代器itr=commons.entrySet().Iterator();itr.hasNext();){
    Entry=itr.next();
    while(i!=entry.getKey()){
    i++;
    }
    if(s2.charAt(i)!=entry.getValue()){
    itr.remove();
    }
    }
    
  • 您尝试这样做的方式可能会导致
    ConcurrentModificationException
    。您应该使用
    迭代器
    ,或者内部使用
    迭代器
  • 虽然可以将
    char
    int
    进行比较,但在比较中可能混淆了键和值。你已经写信了

    if(s2.charAt(i)!=(int)m.getKey())
    
    其中您正在将
    s2.charAt(i)
    整数的键进行比较。从语法上讲,这是正确的,但您可能希望比较值(这是一个
    字符
    ),也就是说,您很可能希望这样做

    if(s2.charAt(i)!=(int)m.getValue())
    
  • 按如下方式操作:

    for (Iterator<Entry<Integer, Character>> itr = commons.entrySet().iterator(); itr.hasNext();) {
        Entry<Integer, Character> entry = itr.next();
    
        while (i != entry.getKey()) {
            i++;
        }
    
        if (s2.charAt(i) != entry.getValue()) {
            itr.remove();
        }
    }
    
    for(迭代器itr=commons.entrySet().Iterator();itr.hasNext();){
    Entry=itr.next();
    while(i!=entry.getKey()){
    i++;
    }
    if(s2.charAt(i)!=entry.getValue()){
    itr.remove();
    }
    }
    
    请出示完整的代码好吗?我的变量是从哪里来的?请出示完整的代码好吗?我从哪里来?谢谢你,哈沙尔。现在说得通了谢谢你,哈沙尔。现在说得通了谢谢你,阿文。现在这个概念对我来说更清楚了,我希望这个解决方案对你有用。不要忘记接受答案,这样将来的访问者也可以自信地使用解决方案。检查以了解如何做。如果有任何疑问/问题,请随时发表评论。谢谢Arvind。现在这个概念对我来说更清楚了,我希望这个解决方案对你有用。不要忘记接受答案,这样将来的访问者也可以自信地使用解决方案。检查以了解如何做。如有任何疑问/问题,请随时发表评论。