如何从hashmap中获取5个最高值? [英] How to get 5 highest values from a hashmap?
问题描述
我有一个Hashmap链接存储为键的zipcodes和作为值存储在hashmap中的填充。
I have a Hashmap that links a zipcodes stored as keys and population stored as values in a hashmap.
hashmap包含大约33k个条目。
The hashmap contains around 33k entries.
我正试图从5个邮政编码中获取5个最高人口值,并打印出与5个最高人口相关联的5个邮政编码,但我无法理解如何算法要做到这一点。
I'm trying to get the 5 highest population values from 5 zip codes and print out the 5 zip codes ASSOCIATED with the 5 highest population, but I'm having trouble understanding the algorithm of how to do it.
如果它只是一个,那么它很容易但是5限制给了我一些麻烦。
If it was just one, its easy but the 5 restriction is giving me some trouble.
我知道将5个值存储在一个int数组中,并且我有一个计数器来确定它们中何时存储了5个,但就是这样。
I know to store the 5 values in an int array and I have a counter to determine when 5 of them are stored, but thats it.
谢谢
int populatedCounter = 0;
int[] populatedZip = new int[5];
it = zipCodePop.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
for (int i = 0; i < populatedZip.length; i++)
{
}
}
}
推荐答案
将这样的条目放入列表和排序是一种选择。但33k元素是一个数字,其中排序的O(n * log(n))复杂性可能已经具有显着的性能影响。
Putting the entries of such a set into a list and sorting it is one option. But 33k elements is a number where the O(n*log(n)) complexity of sorting might already have a noticable performance impact.
一个apporach就是使用nr4bt已经提到的PriorityQueue(我回答时写了这个片段)。它基本上将所有元素插入到PriorityQueue中,该PriorityQueue根据映射条目的值进行排序。
One apporach would be to employ the PriorityQueue that nr4bt already mentioned (I wrote this snippet while he answered). It basically inserts all elements into a PriorityQueue that is sorted according to the values of the map entries.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
public class GreatestOfMap
{
public static void main(String[] args)
{
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("zip000", 1234);
map.put("zip001", 2345);
map.put("zip002", 3456);
map.put("zip003", 4567);
map.put("zip004", 5678);
map.put("zip005", 6789);
map.put("zip006", 123);
map.put("zip007", 234);
map.put("zip008", 456);
map.put("zip009", 567);
map.put("zip010", 7890);
map.put("zip011", 678);
map.put("zip012", 789);
map.put("zip013", 890);
int n = 5;
List<Entry<String, Integer>> greatest = findGreatest(map, 5);
System.out.println("Top "+n+" entries:");
for (Entry<String, Integer> entry : greatest)
{
System.out.println(entry);
}
}
private static <K, V extends Comparable<? super V>> List<Entry<K, V>>
findGreatest(Map<K, V> map, int n)
{
Comparator<? super Entry<K, V>> comparator =
new Comparator<Entry<K, V>>()
{
@Override
public int compare(Entry<K, V> e0, Entry<K, V> e1)
{
V v0 = e0.getValue();
V v1 = e1.getValue();
return v0.compareTo(v1);
}
};
PriorityQueue<Entry<K, V>> highest =
new PriorityQueue<Entry<K,V>>(n, comparator);
for (Entry<K, V> entry : map.entrySet())
{
highest.offer(entry);
while (highest.size() > n)
{
highest.poll();
}
}
List<Entry<K, V>> result = new ArrayList<Map.Entry<K,V>>();
while (highest.size() > 0)
{
result.add(highest.poll());
}
return result;
}
}
这篇关于如何从hashmap中获取5个最高值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!