The 4.0 release of the Apache Commons Collections library has added generics support. I am having trouble converting my code to take advantage of it:
I would like a MultiValueMap
which takes a String as a key, and a collection of Strings as the value. But:
- The keys should retain insertion ordering (so I create the
multi-valued map by decorating a
LinkedHashMap
) - The values should be
unique for each key and retain insertion ordering (so I want the
values Collection type to be a
LinkedHashSet
).
The closest I can get is:
MultiValueMap<String, String> orderedMap = MultiValueMap.multiValueMap(
new LinkedHashMap<String, Collection<String>>(),
LinkedHashSet.class
);
But that produces the error:
The method
multiValueMap(Map<K,? super C>, Class<C>)
in the typeMultiValueMap
is not applicable for the arguments(LinkedHashMap<String,Collection<String>>, Class<LinkedHashSet>)
So now I am in generics hell. Any suggestions would be most welcome.
Prior to version 4.0, I accomplished that with the following:
MultiValueMap orderedMap = MultiValueMap.decorate(
new LinkedHashMap<>(),
LinkedHashSet.class
);
Simple! I provide the LinkedHashMap
to decorate with MultiValueMap
behaviour, and specify the type of collection (LinkedHashSet
) to use as the values. But that requires casting when I call put()
and get()
and so I'd like to be able to use the new generic version provided by 4.0.