Pregunta

I need a TreeMap that can hold multiple values so I chose MultiValueMap from Commons Collections 4.0

With HashMap it's easy

 private MultiValueMap<String, Pair<Integer, String>> foo = 
    new MultiValueMap<String, Pair<Integer, String>>();

but when I want to use some other map implementation like TreeMap, The constructor becomes pretty awkward..

//they hide MultiValueMap(Map map, Factory factory), 
//so I'll have to use static multiValueMap(...) instead

private MultiValueMap<String, Pair<Integer, String>> directoryMap=
    MultiValueMap.multiValueMap(new TreeMap<String, Pair<Integer, String>>());

now, Eclipse throws me this:

The method multiValueMap(Map<K,? super Collection<V>>) in the type MultiValueMap is not 
applicable for the arguments (TreeMap<String,Pair<Integer,String>>)

So, my question is, what does it mean by Map<K,? super Collection<V>>?

I tried new TreeMap<String, ArrayList<Pair<Integer, String>>>() but that didn't work either.

¿Fue útil?

Solución

I believe you're supposed to use

MultiValueMap.multiValueMap(
    new TreeMap<String, Collection<Pair<Integer, String>>>());

...not referring to ArrayList, since presumably the library wants the freedom to choose which collection implementation it puts in the map.

(That said: if you were to use Guava's Multimap instead, you could get this in the much simpler line MultimapBuilder.treeKeys().arrayListValues().build() and get the generics automatically inferred.)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top