4
我有一个Java HashMap,我已经传递给脚本引擎。我想在处理它们时删除条目,因为我稍后会报告无效的键。清除条目的明显通常方法(delete testMap['key'];
)不起作用。如何从Nashorn内部的Java Map中删除元素JavaScript
我该如何通过此测试?
@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());
}
完美!谢谢。 –