次のコードを使用して、ハッシュマップを反復処理しています-ハッシュマップの名前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);
}
}
上記のコードは、特にハッシュマップからのデータの削除に関して正しいですか?プログラムを実行すると、ハッシュマップ内の異なる(ランダムな)値で、(ハッシュマップ内の)特定の値が見つからなかったというエラーメッセージが生成されるためです(このコードは別のものであり、上記のコードと組み合わせて使用されます)。上記のコードを参照すると、ハッシュマップからデータを削除するときのエラーを回避するために、何を覚えておく必要がありますか?