我正在使用以下代码遍历 hashmap- hashmap variable='context' 的名称
Iterator iter = context.keySet().iterator();
while(iter.hasNext())
{
String keystring = (String)iter.next();
String varname= keystring.substring(0, keystring.indexOf("~"));
String level= keystring.substring(keystring.indexOf("~")+1);
Integer levelno= Integer.parseInt(level);
if(levelno==curr_level+1)
{
iter.remove();
//we have found a variable of current level... this has to be removed from hashmap...
//context.remove(keystring);
//also the same variable has to be removed from the script engine as well...
scriptEngine.removeVariable(varname);
}
}
上面的代码是否正确,尤其是关于从哈希图中删除数据?因为当我运行程序时,在哈希图中的不同(随机)值处会生成一条错误消息,指出找不到特定值(在哈希图中)(该代码是单独的并与上述代码结合使用)。参考上面的代码,我必须记住什么,以避免从哈希图中删除数据时出错?