将String,ArrayList等对象添加到List <object>

时间:2015-07-11 21:45:52

标签: java generics

我有一个数据结构Map < String, Map < ConfigKey, ConfigValue<Object > > >。这就像Map的“命名空间”到Map的“关键”到“值”。我的解析器读取配置文件并动态决定该值是否应被视为StringArrayList

我看到错误将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

1 个答案:

答案 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 Javaprogramming 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