如何从hashmap中获取5个最高值?

时间:2014-01-30 19:32:01

标签: java

我有一个Hashmap链接存储为键的zipcodes和作为值存储在hashmap中的填充。

hashmap包含大约33,000个条目。

我正在尝试从5个邮政编码中获取5个最高人口值并打印出与5个最高人口相关联的5个邮政编码,但我无法理解如何执行该算法的算法。

如果它只是一个,那么它很容易但是5限制给我带来了一些麻烦。

我知道将5个值存储在一个int数组中,并且我有一个计数器来确定它们中何时存储了5个,但就是这样。

由于

    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++)
        {

        }
    }

}

5 个答案:

答案 0 :(得分:6)

将这样的集合的条目放入列表并对其进行排序是一种选择。但33k元素是一个数字,其中排序的O(n * log(n))复杂性可能已经具有显着的性能影响。

一个apporach就是使用nr4bt已经提到的PriorityQueue(我回答时写了这个片段)。它基本上将所有元素插入到PriorityQueue中,该PriorityQueue根据映射条目的值进行排序。

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;
    }
}

答案 1 :(得分:4)

尝试使用标准方法并假设人口数在Integer中存储为HashMap

List<Integer> list = new ArrayList<Integer>(zipCodePop.values());
Collections.sort(list, Collections.reverseOrder());
List<Integer> top5 = list.subList(0, 5);

答案 2 :(得分:1)

PriorityQueue也会有所帮助,也是一个关于如何从列表中获得前k个的好主题,您可以查看this link

PriorityQueue<Integer> p = new PriorityQueue<Integer>(5);

int[] a = new int[]{3,5,10,1,23,42,66,1333,545,110};

for (int i : a){
    p.add(i);
    if (p.size() > 5){
        p.poll();
    }
}

//output will be highest 5, [42, 66, 110, 1333, 545]

您可以 O(n log(k))时间复杂度// k是您的最高值。

答案 3 :(得分:1)

这是我做的,希望能为您提供一些您想使用的东西。

flatten = lambda qs: list(map(operator.itemgetter(0), qs))

script = flatten(programas.objects.values_list('script'))
script_eng = flatten(programas.objects.values_list('script_eng'))
# Then you can just zip them together and have what you wanted to begin with
zip_scripts = list(zip(script , script_eng))

getTopByName允许您获得指定名称的首位。

答案 4 :(得分:0)

如果没有电脑,只用一张纸和一支铅笔,你会怎么做?假装你有一堆索引卡上有数字,找到5个最高数字是你的工作。你会怎么做?写下其他人可以遵循的步骤来实现目标,当你写完这些步骤时,你将有一个算法,你可以开始考虑用代码实现。

你说一个单一的最大值很容易,所以它就像你用一个最大值一样,但要跟踪五个最大值。最大值数组可能会有所帮助。