I consulted the Apache Commons Collections mailing list, where it was explained to me that the interface for MultiValueMap
is known to be lacking, but will be revamped in version 4.1 (see here for the JIRA issue and associated discussion).
So in the future we may have a better solution, but in the meantime, as Rohit Jain mentioned in his answer, we're just going to have to suppress some warnings. However, since the key aspect of type safety is for the MultiValueMap
(not the custom collection type), the simplest way to achieve this is:
@SuppressWarnings({ "rawtypes", "unchecked" })
MultiValueMap<String, String> orderedMap =
MapUtils.multiValueMap(new LinkedHashMap(), LinkedHashSet.class);
Note the use of the MapUtils
factory method, rather than the more direct MultiValueMap
which I had used in my original question.