我有一个数据结构Map < String, Map < ConfigKey, ConfigValue<Object > > >
。这就像Map
的“命名空间”到Map
的“关键”到“值”。我的解析器读取配置文件并动态决定该值是否应被视为String
或ArrayList
。
我看到错误将ArrayList
添加到上述数据结构中。请告诉我如何解决这个问题。
private Map<String, Map<ConfigKey, ConfigValue<Object>>> configuration = new HashMap<String, Map<ConfigKey, ConfigValue<Object>>>();
:
configuration.get(groupName).put(new ConfigKey(key), new ConfigValue<Object>(override, value)); // works
:
configuration.get(groupName).put(new ConfigKey(key), new ConfigValue<List>(override, Arrays.asList(values))); // does not work
答案 0 :(得分:3)
var keys = [];
db.marks.find().forEach(function(doc){
for (var key in doc){
if(keys.indexOf(key) < 0){
keys.push(key);
}
}
});
print(keys);
Read more about covariance and contravariance in Java或programming in general.
这会解决它
ConfigValue<List> is not a ConfigValue<Object>.
或
private Map<String, Map<ConfigKey, ConfigValue<? extends Object>>> configuration = new HashMap<String, Map<ConfigKey, ConfigValue<? extends Object>>>();
请避免在代码中使用raw-types。