【问题标题】:How to use NavigableMap features with Guava's Multimap (with asMap())?如何在 Guava 的 Multimap 中使用 NavigableMap 功能(使用 asMap())?
【发布时间】:2014-05-08 08:02:43
【问题描述】:

假设我有这样的东西:

Multimap<Integer, Integer> data = TreeMultimap.create();

如何在我的数据上使用.headMap()?我想,TreeMultimap.asMap() 是要走的路。

文档 (link) 说,TreeMap.asMap() 返回 NavigableMap&lt;K,Collection&lt;V&gt;&gt;,但我无法让它工作。

NavigableMap<Integer, ArrayList<Integer>> test = data.asMap(); // type mismatch
SortedMap<Integer, ArrayList<Integer>> test = data.asMap(); // type mismatch

我做错了什么?

谢谢!

PS:我使用的是番石榴 16

【问题讨论】:

    标签: java collections guava multimap


    【解决方案1】:

    data 的类型是 Multimap,而不是 TreeMultimap。另外,NavigableMap&lt;Integer, Collection&lt;Integer&gt;&gt;NavigableMap&lt;Integer, ArrayList&lt;Integer&gt;&gt; 不兼容。

    将您的代码更改为:

    TreeMultimap<Integer, Integer> data = TreeMultimap.create();
    NavigableMap<Integer, Collection<Integer>> test = data.asMap();
    

    【讨论】:

    • 感谢您在这么短的时间内回答!
    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2015-04-30
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多