3

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.

tom91136
  • 8,662
  • 12
  • 58
  • 74

1 Answers1

3

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.)

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • "not referring to `ArrayList`, since presumably the library wants the freedom to choose..." In fact, an `ArrayList` will be used by default (see [the javadoc](http://commons.apache.org/proper/commons-collections/javadocs/api-release/index.html)). To use a different implementation, see [this question](http://stackoverflow.com/questions/22625575). – Ryan Bennetts Apr 01 '14 at 03:26