java 获取哈希图中的前 10 个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15436516/
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 top 10 values in hash map
提问by Tohmas
I am trying to figure out how could I get the top 10 values from the HashMap
. I was initially trying to use the TreeMap
and have it sort by value and then take the first 10 values however it seems that that is not the option, as TreeMap
sorts by key.
我想弄清楚如何从HashMap
. 我最初尝试使用TreeMap
并让它按值排序,然后取前 10 个值,但似乎这不是选项,因为TreeMap
按键排序。
I want to still be able to know which keys have the highest values, the K, V
of the map are String, Integer
.
我希望仍然能够知道哪些键具有最高值,K, V
地图的String, Integer
.
回答by sk2212
Maybe you should implement the Comparable
Interface to your value objects stored in the hashmap.
Then you can create a array list of all values:
也许您应该Comparable
为存储在哈希图中的值对象实现接口。然后您可以创建所有值的数组列表:
List<YourValueType> l = new ArrayList<YourValueType>(hashmap.values());
Collection.sort(l);
l = l.subList(0,10);
Regards
问候
回答by Biswajit
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Testing {
public static void main(String[] args) {
HashMap<String,Double> map = new HashMap<String,Double>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);
map.put("A",99.5);
map.put("B",67.4);
map.put("C",67.4);
map.put("D",67.3);
System.out.println("unsorted map: "+map);
sorted_map.putAll(map);
System.out.println("results: "+sorted_map);
}
}
class ValueComparator implements Comparator<String> {
Map<String, Double> base;
public ValueComparator(Map<String, Double> base) {
this.base = base;
}
// Note: this comparator imposes orderings that are inconsistent with equals.
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return -1;
} else {
return 1;
} // returning 0 would merge keys
}
}
回答by Sebastian D'Agostino
I base my answer in this one from sk2212
我的答案基于sk2212
First you need to implement a descending comparator:
首先你需要实现一个降序比较器:
class EntryComparator implements Comparator<Entry<String,Integer>> {
/**
* Implements descending order.
*/
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
if (o1.getValue() < o2.getValue()) {
return 1;
} else if (o1.getValue() > o2.getValue()) {
return -1;
}
return 0;
}
}
Then you can use it in a method such as this one for the attribute "hashmap":
然后,您可以将它用于属性“hashmap”的方法中:
public List<Entry<String,Integer>> getTopKeysWithOccurences(int top) {
List<Entry<String,Integer>> results = new ArrayList<>(hashmap.entrySet());
Collections.sort(results, new EntryComparator());
return results.subList(0, top);
}
回答by Jirawat Uttayaya
Let's assume you have a Map, but this example can work for any type of
假设您有一个 Map,但是这个例子适用于任何类型的
Map<String, String> m = yourMethodToGetYourMap();
List<String> c = new ArrayList<String>(m.values());
Collections.sort(c);
for(int i=0 ; i< 10; ++i) {
System.out.println(i + " rank is " + c.get(i));
}
回答by NPE
回答by aymeric
If you are trying to get the 10 highest values of the map (assuming the values are numeric or at least implementing Comparable) then try this:
如果您试图获得地图的 10 个最高值(假设这些值是数字或至少实现了 Comparable),请尝试以下操作:
List list = new ArrayList(hashMap.values());
Collections.sort(list);
for(int i=0; i<10; i++) {
// Deal with your value
}