我正在使用一个递归的hashmaps树,特别是Hashmap map,其中Object是对另一个Hashmap的引用,依此类推.这将通过递归算法传递:
foo(String filename, Hashmap<String, Object> map)
{
//some stuff here
for (Entry<String, Object> entry : map.entrySet())
{
//type warning that must be suppressed
foo(entry.getKey(), (HashMap<String, Object>)entry.getValue());
}
}
Run Code Online (Sandbox Code Playgroud)
我知道肯定Object是类型,Hashmap<String, Object>但我很恼火,我必须使用抑制警告@SuppressWarnings("unchecked").
我会对一个解决方案感到满意,该解决方案可以执行assert(/*entry.getValue() is of type HashMap<String, Object>*/)或者抛出异常.我沿着Generics路线进行编译类型安全,如果我压制警告那么它就会失败.
谢谢你的意见,ksb