1

I'm trying to find a pre-built implementation of a MultiValuedTreeMap. In a nutshell, I need a single key to map to multiple values via the use of a List. I then need each key to be stored in a sorted structure, what I imagine should be a tree map - hence the name.

The closest I have found is a ArrayListValuedHashMap: https://commons.apache.org/proper/commons-collections/javadocs/api-4.4/index.html

I see no version which uses a TreeMap instead of a HashMap, however.

I am new to the Apache Commons libraries so forgive me if the question is obvious / already solved.

I am also aware I can implement this relatively easily myself, but I don't want to reinvent the wheel if it already exists.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
goastler
  • 223
  • 2
  • 6

1 Answers1

0

There is no direct implementation of ListValuedTreeMap out of the box. To pass concrete implementation of List or Map into ListValuedMap is intended class AbstractListValuedMap

From javadoc:

Subclasses specify a Map implementation to use as the internal storage and the List implementation to use as values.

Generic implementation of ListValuedTreeMap can be implemented this way:

public class ListValuedTreeMap<K extends Comparable<K>, V> extends AbstractListValuedMap<K, V> {
    public ListValuedTreeMap() {
        super(new TreeMap<>());
    }

    @Override
    protected List<V> createCollection() {
        return new ArrayList<>();
    }
}
Bedla
  • 4,789
  • 2
  • 13
  • 27