繁体   English   中英

如果Java中的HashMap键相等,如何获取2个最大值

[英]How to take 2 maximum values if HashMap key is equal in Java

我有一个HashMap,我想从中获取两个最大值。 我使用以下代码。 但是,如果HashMap键相等,则它不会给出正确的值。 那么,如何采取正确的价值观?

import java.util.LinkedHashMap;
import java.util.Map;

public class TakeTwoMaximumAndChange {
    public static void main(String[] args) {
        TakeTwoMaximumAndChange ob = new TakeTwoMaximumAndChange();
        ob.test();
    }

    public void test() {
        LinkedHashMap<String, Double> data = new LinkedHashMap<String, Double>();
        data.put("a", 2.3);
        data.put("b", 2.5);
        data.put("c", 8.3);
        data.put("d", 3.8);
        data.put("c", 6.3);
        data.put("f", 4.4);

        Map.Entry<String, Double> max1 = null;
        Map.Entry<String, Double> max2 = null;

        // searching the first biggest value
        for (Map.Entry<String, Double> en : data.entrySet()) {
            if (max1 == null || en.getValue().compareTo(max1.getValue()) > 0) {
                max1 = en;
            }
        }
        System.out.println(max1);

        // searching the second biggest value
        for (Map.Entry<String, Double> en : data.entrySet()) {
            if (en != max1
                    && (max2 == null || (en.getValue().compareTo(max2.getValue())) > 0)) {
                max2 = en;
            }
        }
        System.out.println(max2);
    }
}

快速回答是

// searching the first and second biggest value at once
        for (Map.Entry<String, Double> en : data.entrySet()) {
            if (max1 == null || en.getValue().compareTo(max1.getValue()) > 0) {
                  max2= max1;
                  max1 = en;

            }
        }

但是,如果测试的第一个条目是最大值,则会失败(感谢您指出这一点,@ Grayson)

// searching the first and second biggest value at once, corrected
        for (Map.Entry<String, Double> en : data.entrySet()) {

            if (max1 == null){
                  max1 = en;
            }else if (en.getValue().compareTo(max1.getValue()) > 0) {
                  max2= max1;
                  max1 = en;

            }else if ( (max2 == null) || (en.getValue().compareTo(max2.getValue()) > 0) ){
                  max2 = en;
            }
        }

您的问题与您的算法无关。 这是因为Map不接受重复的密钥。

LinkedHashMap<String, Double> data = new LinkedHashMap<String, Double>();
data.put("c", 8.3);
data.put("c", 6.3); // The second value replace the first one in the Map



注意: 与问题无关 ,但如果您使用的是Java 8,则可以考虑使用lambda,我认为它更具可读性:

Map<String, Double> m = new HashMap<>();        

final Map.Entry<String, Double> max = m.entrySet()
    .stream()
    .max((o1, o2) -> o1.getValue().compareTo(o2.getValue())) // find the max
    .get();

final Map.Entry<String, Double> max2 = m.entrySet()
    .stream()
    .filter((e) -> !e.getKey().equals(max.getKey())) // remove the first max
    .max((o1, o2) -> o1.getValue().compareTo(o2.getValue()))
    .get();

我不能直接使用HashMap.So,我对HAshMap值进行排序,然后将值添加到ArrayList,并将列表的最后2个值作为最大2个值。

暂无
暂无

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

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