将Map.Entry对象与双变量进行比较

时间:2015-04-20 01:55:43

标签: java dictionary iterator

这是一个基本问题。我正在使用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

1 个答案:

答案 0 :(得分:1)

您可以采用的方法是使用泛型声明Map和Iterator。这样,条目值将被输入为double。

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

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