【问题标题】:Comparing Map.Entry object with a double variable将 Map.Entry 对象与双变量进行比较
【发布时间】:2015-06-26 14:30:14
【问题描述】:

这是一个基本问题。我正在使用iterator 迭代地图,并且我有一个双变量m_asim。我需要知道如何将 map 的值与 double 变量进行比较?

我的代码:

for(Map mp:dblist){
            Iterator it = mp.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry pair = (Map.Entry)it.next();
                    System.out.println(pair.getKey() + " = " + pair.getValue());
                    //Need to know how to compare in next line
                    if(pair.getValue() >= m_asim) // this line give me error
                    {}

                    it.remove(); 
                }
        }

错误:

operator > is undefined for the argument type(s) Object,double

【问题讨论】:

  • 您希望 pair.getValue() 是什么类型?
  • 我希望它是“双”
  • 如果是这种情况,您需要地图的类型及其条目来反映这一点。你只有MapMap.Entry,而不是Map<someclass, Double>Map.Entry<someclass, Double>

标签: java dictionary iterator


【解决方案1】:

您可以采用的一种方法是使用泛型声明 Map 和 Iterator。这样,条目值将被键入为双精度值。

假设 Map 有一个字符串键和一个双精度值,你可以这样做:

    Iterator<Entry<String, Double>> it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, Double> pair = it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
        //Need to know how to compare in next line
        if(pair.getValue() >= m_asim) // this line give me error
        {}

        it.remove(); 
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2012-01-21
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多