Guava ImmutableSortedSetMultimap?

Google Guava has SortedSetMultimap

. Wonderful. Now where is the unchanged version? There is ImmutableSetMultimap

. But what about ImmutableSortedSetMultimap

? (Please don't answer "Why do you need this?")


source to share

3 answers

It doesn't actually implement SortedSetMultimap

, but ImmutableSetMultimap.Builder

has a method orderValuesBy(Comparator)

that can be used, resulting in collections of ImmutableSortedSet




If you don't need extra meaning Multimap

, you have an indirect approach (I wrote the code in SO so it may not work, but there is an idea):

SortedSetMultimap<K,V> set = ...;
ImmutableMap<K, ImmutableSortedSet<V>> result = ImmutableMap.copyOf(Maps.transform(set.asMap(), new Function<SortedSet<V>, ImmutableSortedSet<V>>() {
  public ImmutableSortedSet<V> apply(SortedSet<V> s) {
    return ImmutableSortedSet.copyOf(s);


That is: turn yours SortedSetMultimap

into Map<K,SortedSet<V>>

, then Map<K,ImmutableSortedSet<V>>

, and then ImmutableMap


And I don't know enough Guava, but since it is ImmutableSetMultimap

immutable, the order of the copied set can remain: this means there is no need ImmutableSortedSetMultimap

for navigation / iteration (unless you require a specific method SortedSet




I was looking for this myself and just discovered this: Multimaps.unmodifiableSortedSetMultimap ()

Not what we're looking for, but good enough in my case.



All Articles