commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Neidhart <>
Subject Re: [collections] MultiValueMap with a custom value collection type
Date Thu, 27 Mar 2014 07:26:47 GMT
On 03/27/2014 12:59 AM, Ryan Bennetts wrote:
> Hello all
> I am having trouble understanding how to use a custom value collection type
> with a MultiValueMap. I want to use the new generics-supported version, but
> can't see how to do this without excessive casting, which seems to defeat a
> lot of the purpose of using generics in the first place. I have posted this
> issue on stackoverflow <> but
> thought I would ask here too.
> I would like a MultiValueMap<>which
> takes a String as a key, and a collection of Strings as the value.
> But:
>    1. The keys should retain insertion ordering (so I create the
>    multi-valued map by decorating a
> LinkedHashMap<>
>    )
>    2. 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 this error:
> The method multiValueMap(Map<K,? super C>, Class<C>) in the type
> MultiValueMap 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. One
> suggestion I have received is to use:
> @SuppressWarnings("unchecked")MultiValueMap<String, String> orderedMap
> = MultiValueMap.multiValueMap(
>     new LinkedHashMap<String, Collection<String>>(),
>     (Class<LinkedHashSet<String>>)(Class<?>)LinkedHashSet.class);
> This works but do I really have to do two casts? Is this really what the
> api designers intended?
> Prior to version 4.0, I accomplished that with the following:
> MultiValueMap orderedMap = MultiValueMap.decorate(
>     new LinkedHashMap<>(),
>     LinkedHashSet.class);
> Which is certainly simpler, but not type safe. 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.

Hi Ryan,

we know that the MultiValueMap is flawed for several reasons, but we are
working on adding a revamped variant in 4.1, see the issue:

The best thing you can do right now is to instantiate it in the
pre-generics way:

@SuppressWarnings({ "rawtypes", "unchecked" })
MultiValueMap<String, String> orderedMap
  = MapUtils.multiValueMap(new LinkedHashMap(), LinkedHashSet.class);

You actually need type-safety for orderedMap, not for the argument to
multiValueMap. The need to suppress warnings is annoying, but with 4.1
this should also go away.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message