我有一个Java HashMap,将其传递给脚本引擎。由于我稍后会报告无效的键,因此希望在处理条目时删除它们。删除条目的常见方法(delete testMap['key'];
)似乎没有效果。
如何让这个测试通过?
@Test
public void mapDelete() throws ScriptException{
Map<String,String> map = new HashMap<>(1);
map.put("key","value");
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
engine.getContext().getBindings(ScriptContext.GLOBAL_SCOPE).put ("testMap", map);
engine.eval("delete testMap['key'];");
Assert.assertEquals (0, map.size());
}