Garret Wilson
Garret Wilson

Reputation: 21436

Guava ImmutableSortedSetMultimap?

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?")

Upvotes: 5

Views: 975

Answers (3)

KevinT
KevinT

Reputation: 76

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.

Upvotes: 2

Louis Wasserman
Louis Wasserman

Reputation: 198211

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.

Upvotes: 9

NoDataFound
NoDataFound

Reputation: 11959

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).

Upvotes: 0

Related Questions