2

I have a data structure Map < String, Map < ConfigKey, ConfigValue<Object > > >. This is like a Map of "namespace" to Map of "key" to "value". My parser reads config file and dynamically decides if the value should be treated as String or ArrayList.

I see error adding ArrayList to above data structure. Please show me how to fix this.

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
  • "I am facing trouble" is not a good error description. Please amend. Commented Jul 11, 2015 at 22:33

1 Answer 1

3
ConfigValue<List> is not a ConfigValue<Object>.

Read more about covariance and contravariance in Java or in programming in general.

This would fix it

private Map<String, Map<ConfigKey, ConfigValue<? extends Object>>> configuration = new HashMap<String, Map<ConfigKey, ConfigValue<? extends Object>>>();

or

Map<String, Map<ConfigKey, ConfigValue<?>>> 

And please avoid raw-types in your code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.