javaapache-commons-collection

No standard wrapper implementation for MultiValuedMap


Am I missing something, or is there really no default implementation of Apache's new MultiValuedMap that could be used to simply wrap provided Map<K, ? extends Collection<V>>?

In other words, I am looking for a direct replacement of the now deprecated MapUtils.multiValueMap factory method: none of existing implementations like HashSetValuedHashMap meet my needs as I need to use IdentifyHashMap as backend map


Solution

  • In the end I had to implement necessary helper method myself:

    public static <K, V, C extends Collection<V>> MultiValuedMap<K, V> multiValueMap(Map<K, C> map,
            final Factory<C> collectionFactory) {
        return new AbstractMultiValuedMap<K, V>(map) {
            @Override
            protected Collection<V> createCollection() {
                return collectionFactory.create();
            }
        };
    }
    

    Though, with how trivial the implementation is, I cannot believe it is not there in commons-collections already.