简体   繁体   English

从列表中查找最少 3 个值

[英]Finding Minimum 3 values from a list

I am working on project where i need to find the minimum 3 three values from a HashMap .我正在研究需要从HashMap 中找到最少 3 个三个值的项目。 I have found a code that finds highest values.我找到了一个可以找到最高值的代码。 Here it is.这里是。

public 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>>()
    {
        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 (java.util.Map.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;
    }

How cam I modify it to find the minimum values?我如何修改它以找到最小值? Kindly help me here.请在这里帮助我。 UPDATE :Sorry for my mistake, I was looking to find minimum values from a HashMap更新:抱歉我的错误,我想从HashMap 中找到最小值

就个人而言,我会简单地通过Collections.sort( ... ) (因为它似乎由 Comparables 组成Collections.sort( ... )对列表进行排序,然后返回三个第一个(或最后一个)元素。

You could use exactly the same code except reverse the order of the Comparator using Collections.reverseOrder(Comparator c) .除了使用Collections.reverseOrder(Comparator c)反转Comparator的顺序之外,您可以使用完全相同的代码。

PriorityQueue<Entry<K, V>> highest = 
    new PriorityQueue<Entry<K,V>>(n, Collections.reverseOrder(comparator));

However, that would not be the most efficient approach.然而,这并不是最有效的方法。 It is possible to do this in one pass.可以一次性完成此操作。

Simply you can try like this.简单地你可以像这样尝试。 You don't need to have a customized comparator to achieve what you are trying here.您不需要使用自定义比较器来实现您在此处尝试的功能。

    ArrayList<Integer> list = new ArrayList<Integer>();

    list.add(23);
    list.add(65);
    list.add(85);
    list.add(1);
    list.add(54);
    list.add(5);

    Collections.sort(list);

    for(int i=0;i<3;i++)
        System.out.println(list.get(i));

您可以使用Collections.min(list)从列表中获取最低值

The suggestions involving sorting the map have a runtime of O(n log(n)).涉及对地图排序的建议的运行时间为 O(n log(n))。 The following implementation has a runtime of O(nm), where n is the size of the map and m is the size of the result, ie if you want the m smallest elements.以下实现的运行时间为 O(nm),其中 n 是地图的大小,m 是结果的大小,即如果您想要 m 个最小元素。

public static <K, V extends Comparable<? super V>> List<Entry<K, V>> findGreatest(Map<K, V> map, int m)
{
    LinkedList<Entry<K, V>> result = new LinkedList<Entry<K, V>>();

    for (Entry<K, V> entry : map.entrySet())
    {
        ListIterator<Entry<K, V>> iterator = result.listIterator();
        int i = n;
        while (iterator.hasNext() && i-- > 0)
        {
            if (iterator.next().getValue().compareTo(entry.getValue()) > 0)
            {
                iterator.previous();
                iterator.add(entry);
                if (result.size() > n)
                {
                    result.removeLast();
                }
                break;
            }
        }

        if (result.size() < n)
        {
            result.add(entry);
        }
    }

    return result;
}

@Jude Niroshan, his answer also correct one and you can try this also if it helps, you can change the values and check. @Jude Niroshan,他的回答也正确,如果有帮助,您也可以尝试一下,您可以更改值并检查。

LinkedList<Integer> li=new LinkedList<Integer>();
    li.add(-8);
    li.add(0);
    li.add(10);
    li.add(-1);
    li.add(9);
Collections.sort(li);
for(Integer a:li){
        if(a<3)
        System.out.println(a);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM