Java 获取地图的最小值(键,双)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2776176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get minvalue of a Map(Key,Double)
提问by user326667
Is there a method (maybe with Google Collections) to obtain the min value of a Map(Key, Double)
?
是否有一种方法(可能使用 Google Collections)来获取 a 的最小值Map(Key, Double)
?
In the traditional way, I would have to sort the map according to the values, and take the first/last one.
以传统方式,我必须根据值对地图进行排序,并取第一个/最后一个。
采纳答案by BalusC
You can use the standard Collections#min()
for this.
您可以Collections#min()
为此使用标准。
Map<String, Double> map = new HashMap<String, Double>();
map.put("1.1", 1.1);
map.put("0.1", 0.1);
map.put("2.1", 2.1);
Double min = Collections.min(map.values());
System.out.println(min); // 0.1
Update: since you need the key as well, well, I don't see ways in Collections
or Google Collections2
API since a Map
is not a Collection
. The Maps#filterEntries()
is also not really useful, since you only know the actual result at endof iteration.
更新:因为您也需要密钥,所以我看不到Collections
Google Collections2
API 的方法,因为 aMap
不是Collection
. 该Maps#filterEntries()
也不是真正有用的,因为你只知道在实际的结果结束迭代。
Most straightforward solution would then be this:
最直接的解决方案是这样的:
Entry<String, Double> min = null;
for (Entry<String, Double> entry : map.entrySet()) {
if (min == null || min.getValue() > entry.getValue()) {
min = entry;
}
}
System.out.println(min.getKey()); // 0.1
(nullcheck on min
left aside)
(min
左边的空检查)
回答by Michael Borgwardt
In traditional way, I would have to sort the map according to the values, and take the first/last one. thanks
以传统方式,我必须根据值对地图进行排序,并取第一个/最后一个。谢谢
No, you wouldn't. You would have to iterate through all values and at each step compare the current element with the smallest one seen so far. That's O(n), compared with O(n*log(n)) for sorting - a potentially hugedifference.
不,你不会。您必须遍历所有值,并在每一步将当前元素与目前看到的最小元素进行比较。这是 O(n),与用于排序的 O(n*log(n)) 相比 - 潜在的巨大差异。
BTW, this is exactly how Collections.min()
works.
顺便说一句,这正是Collections.min()
工作原理。
回答by Eyal Schneider
In order to do it efficiently, you may want to define your own data structure, such that it implements the Map interface,but also allows efficient getMin() operation.
为了有效地做到这一点,您可能需要定义自己的数据结构,使其实现 Map 接口,但也允许高效的 getMin() 操作。
This can be done using two internal data structures: a map and a tree (or heap data structure). Each time a new pair (K,V) is added, add them to the map, and also to the tree (as a single entry). This allows O(1) time for get(Key) operations, and O(log n) time for addition, removal, and getMin operations.
这可以使用两个内部数据结构来完成:映射和树(或堆数据结构)。每次添加新对 (K,V) 时,将它们添加到地图和树中(作为单个条目)。这使得 get(Key) 操作的时间为 O(1),添加、删除和 getMin 操作的时间为 O(log n)。
回答by Yishai
I'd be inclined to use a Google Collections BiMap:
我倾向于使用 Google Collections BiMap:
String minKey = HashBiMap.create(map).inverse().get(Collections.min(map.values()));
Or something like that (not tested).
或类似的东西(未测试)。
回答by superfav
You still can use Collections.min
with a custom Comparator
to get the Map.Entry
with the lower value:
您仍然可以使用Collections.min
自定义Comparator
来获得Map.Entry
较低的值:
Map<String, Double> map = new HashMap<String, Double>();
map.put("1.1", 1.1);
map.put("0.1", 0.1);
map.put("2.1", 2.1);
Entry<String, Double> min = Collections.min(map.entrySet(), new Comparator<Entry<String, Double>>() {
public int compare(Entry<String, Double> entry1, Entry<String, Double> entry2) {
return entry1.getValue().compareTo(entry2.getValue());
}
});
System.out.printf("%s: %f", min.getKey(), min.getValue()); // 0.1: 0.100000
With Java 8:
使用 Java 8:
Entry<String, Double> min = Collections.min(map.entrySet(),
Comparator.comparing(Entry::getValue));
回答by Tarrasch
Using java 8 (and static imports). We can make @superfav's solution much tidier:
使用 java 8(和静态导入)。我们可以使@superfav 的解决方案更加整洁:
Map<String, Double> myMap;
String theKeyWithHighestValue = Collections.min(myMap.entrySet(), comparingDouble(Entry::getValue)).getKey()
回答by voho
Using Java 8 streams:
使用 Java 8 流:
return map
.entrySet()
.stream()
.sorted(Comparator.comparingDouble(Map.Entry::getValue))
.findFirst()
.map(Map.Entry::getValue);
Or
或者
return map
.entrySet()
.stream()
.min(Comparator.comparingDouble(Map.Entry::getValue))
.map(Map.Entry::getValue);
But if you want to do it multiple times, then definitely give heapa look.
但是如果你想多次这样做,那么一定要看看堆。
回答by Az.MaYo
In Java 8 we can get easily:
在 Java 8 中,我们可以轻松获得:
Double minValue = map.entrySet().stream().min(Map.Entry.comparingByValue()).get().getValue();
Double maxValue = map.entrySet().stream().max(Map.Entry.comparingByValue()).get().getValue();
回答by Ankit Sharma
Java8 One-Liner
Java8 单线
Key key = Collections.min(map.entrySet(), Map.Entry.comparingByValue()).getKey()