Guava:通过反转Map构造一个Multimap [英] Guava: construct a Multimap by inverting a Map
本文介绍了Guava:通过反转Map构造一个Multimap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么Guava没有下面的工厂调用从正常的Map创建一个MultiMap?
why does Guava doesn't have the following factory call to create a MultiMap from a normal Map?
public static <K,V> MultiMap<K,V> invertMap(Map<V,K> map);
我有程序名称映射到调用它们的频率的整数。我想反转这个,所以我可以最终构造一个TreeMap,按调用计数排序,然后是导致一个或多个程序名称的键。
I have program-names mapped to an integer of how often they were called. I'd like to invert this, so that i can ultimately construct a TreeMap, sorted by call-count, which then are the keys leading to one or multiple program-names.
推荐答案
如何:
public static <K,V> Multimap<K,V> invertMap(Map<V,K> map) {
return Multimaps.invertFrom(Multimaps.forMap(map), ArrayListMultimap.create());
}
似乎不需要专门的函数。您甚至可以很容易地回到 TreeMap
:
Doesn't seem like this requires a dedicated function. You can even get back to a TreeMap
pretty easily:
Map<String, Integer> programCounts;
TreeMap<Integer, Collection<String>> map =
new TreeMap<>(
Multimaps.invertFrom(
Multimaps.forMap(programCounts),
ArrayListMultimap.create()
).asMap()
);
这篇关于Guava:通过反转Map构造一个Multimap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文