Jiew Meng
Jiew Meng

Reputation: 88267

Casting multimap from Multimaps.synchronizedSortedSetMultimap

I am doing

private TreeMultimap<Integer, String> top10words = 
    Multimaps.synchronizedSortedSetMultimap(TreeMultimap.create());

But I get

Type mismatch: cannot convert from SortedSetMultimap to TreeMultimap

What should I be doing here? I tried casting but that failed

private TreeMultimap<Integer, String> top10words = 
    (TreeMultimap<Integer, String>) Multimaps.synchronizedSortedSetMultimap(TreeMultimap.create());

Upvotes: 1

Views: 113

Answers (1)

Grzegorz Rożniecki
Grzegorz Rożniecki

Reputation: 28015

What you should really do is look at type hierarchy for TreeMultimap and note that

  • it extends AbstractSortedKeySortedSetMultimap
  • which extends AbstractSortedSetMultimap
  • which implements SortedSetMultimap.

The latter (SortedSetMultimap) is what is returned (actually it's SynchronizedSortedSetMultimap - an internal Guava class), not TreeMultimap (which isn't synchronized) and that's why only

SortedSetMultimap<Integer, String> top10words =
    Multimaps.synchronizedSortedSetMultimap(TreeMultimap.create());

is possible.

Upvotes: 2

Related Questions