5

Google Guava has a SortedSetMultimap. Wonderful. Now where is the immutable version? There exists an ImmutableSetMultimap. But what about ImmutableSortedSetMultimap? (Please don't reply with "Why do you want one?")

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • Sir, elaborate some more on the topic. If I recall correctly, the nature of a set somewhat conflicts with ordering? – Caffeinated Sep 10 '14 at 22:51
  • 2
    You mean, `TreeSet` doesn't exist? – biziclop Sep 10 '14 at 22:52
  • 3
    To answer the question, there indeed doesn't appear to be one, even though it's a perfectly reasonable thing to have. Maybe you should submit a feature request. – biziclop Sep 10 '14 at 22:54

3 Answers3

9

It won't actually implement SortedSetMultimap, but ImmutableSetMultimap.Builder has an orderValuesBy(Comparator) method that you can use, which has the effect that the value collections are ImmutableSortedSets.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

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

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

KevinT
  • 76
  • 3
0

If you don't need the supplementary meaning of Multimap, you have the indirect approach (I wrote the code in SO, so it might not work, but the idea is there):

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 your SortedSetMultimap into a Map<K,SortedSet<V>>, then a Map<K,ImmutableSortedSet<V>>, and then an ImmutableMap.

And I don't know enough Guava, but since the ImmutableSetMultimap is immutable, the ordering of copied set might remain: that would mean there is absolutely no need for a ImmutableSortedSetMultimap for navigation/iteration (apart if you require specific method of SortedSet).

NoDataFound
  • 11,381
  • 33
  • 59