Java 从Multimaps.synchronizedSortedSetMultimap转换multimap

Java 从Multimaps.synchronizedSortedSetMultimap转换multimap,java,guava,Java,Guava,我在做什么 private TreeMultimap<Integer, String> top10words = Multimaps.synchronizedSortedSetMultimap(TreeMultimap.create()); private TreeMultimap top10words= synchronizedSortedSetMultimap(TreeMultimap.create()); 但我明白了 类型不匹配:无法从SortedSetMulti

我在做什么

private TreeMultimap<Integer, String> top10words = 
    Multimaps.synchronizedSortedSetMultimap(TreeMultimap.create());
private TreeMultimap top10words=
synchronizedSortedSetMultimap(TreeMultimap.create());
但我明白了

类型不匹配:无法从SortedSetMultimap转换为TreeMultimap

我应该在这里做什么?我试过选角,但失败了

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

您真正应该做的是查看的类型层次结构,并注意

  • 它扩展了AbstractSortedKeySortedSetMultimap
  • 它扩展了AbstractSortedSetMultimap
  • 它实现了
    SortedSetMultimap
后者(
SortedSetMultimap
)是返回的(实际上它是一个内部番石榴类),而不是
TreeMultimap
(它不是同步的),这就是为什么

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

是可能的。

您希望在这里实现什么?您正在调用的方法返回一个
SynchronizedSortedSetMultimap
,而不是
TreeMultimap
,它用互斥锁将其包装-您不能将其回滚。
TreeMultimap
是一个
SortedSetMultimap
,但并不是所有的
SortedSetMultimap
s都是
TreeMultimap
s。试试这个:
TreeMultimap t=(TreeMultimap)Multimaps.synchronizedSortedSetMultimap((TreeMultimap)TreeMultimap.create())
or
private TreeMultimap top10words=(TreeMultimap)Multimaps.synchronizedSortedSetMultimap(TreeMultimap.create())@RAHULROY后者不起作用。