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())
orprivate TreeMultimap top10words=(TreeMultimap)Multimaps.synchronizedSortedSetMultimap(TreeMultimap.create())代码>@RAHULROY后者不起作用。