当我从TreeMap打印出值时,为什么会得到一个空值列表?

时间:2013-01-21 03:14:58

标签: java

我是Java新手并使用本网站上的TreeMap代码示例但是当我尝试迭代TreeMap时,我得到一个空值列表但是当我直接打印地图时我可以看到键/价值对。我该如何纠正这个?

import java.util.*;
public class Testing {

    public static void main(String[] args) {

        HashMap<String,Double> map = new HashMap<String,Double>();
        ValueComparator1 bvc =  new ValueComparator1(map);
        TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);

        map.put("A",99.5);
        map.put("B",67.4);
        map.put("C",67.4);
        map.put("D",67.3);

        System.out.println("unsorted map: "+map);

        sorted_map.putAll(map);


        System.out.println("results: "+sorted_map);

        for(String key: sorted_map.keySet())
        {
            System.out.println(sorted_map.get(key)); //null values-Why?
        }
    }
}

class ValueComparator1 implements Comparator<String> {

    Map<String, Double> base;
    public ValueComparator1(Map<String, Double> base) {
        this.base = base;
    }

    // Note: this comparator imposes orderings that are inconsistent with equals.    
    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        } // returning 0 would merge keys
    }
}

4 个答案:

答案 0 :(得分:6)

它不起作用,因为当给定相同的键时比较器不返回0,例如比较(“A”,“A”)。将其更改为

    public int compare(String a, String b) {
        Double va = base.get(a);
        Double vb = base.get(b);
        if(va > vb) {
            return -1;
        } else if(va < vb) {
            return 1;
        } else {
            return a.compareTo(b);
        }
    }

它会起作用。

答案 1 :(得分:3)

对不起,但你的例子有点颠倒了。您将键放入有序映射(树映射),然后使用值作为键,您可以通过值进行比较。看起来你正在寻找处理具有键和值的对象,所以这是你可能想要考虑的事情。这肯定是处理“复合”概念的OOP方式,就像你用地图建模的概念一样。

class Pair implements Comparable<Pair> {
    String value;
    double key;

    Pair(String value, double key) {
        this.value = value;
        this.key = key;
    }

    public int compareTo(Pair p) {
        return Double.compare(key, p.key);
    }

    public String toString(Pair p) {
        return value + "," + key;
    }
}

static void main(String[] args) {
    Set<Pair> unsortedSet = new HashSet<Pair>();
    unsortedSet.add(new Pair("A", 99.5));
    unsortedSet.add(new Pair("B", 67.4));
    unsortedSet.add(new Pair("C", 67.4));
    unsortedSet.add(new Pair("D", 67.3));

    Set<Pair> sortedSet = new TreeSet<Pair>();
    sortedSet.add(new Pair("A", 99.5));
    sortedSet.add(new Pair("B", 67.4));
    sortedSet.add(new Pair("C", 67.4));
    sortedSet.add(new Pair("D", 67.3));

    System.out.println("Unsorted set: " + unsortedSet);
    System.out.println("Sorted set: " + sortedSet);

    for (Pair pair : sortedSet) {
        System.out.println(pair);
    }
}

答案 2 :(得分:1)

由于比较器永远不会返回0,因此TreeMap.get()不起作用。你仍然可以迭代这样的TreeMap条目

    for (Entry<String, Double> e : sorted_map.entrySet()) {
        System.out.println(e);
    }

打印

A=99.5
C=67.4
B=67.4
D=67.3

答案 3 :(得分:0)

Adam Crume的代码非常重要。要向您解释更多相关信息,当您致电sorted_map.get(key)时,会转到java.util.TreeMap class',getEntryUsingComparator方法,因为您已明确设置了比较器。这个方法看起来像

final Entry<K,V> getEntryUsingComparator(Object key) {
        K k = (K) key;
        Comparator<? super K> cpr = comparator;
        if (cpr != null) {
            Entry<K,V> p = root;
            while (p != null) {
                int cmp = cpr.compare(k, p.key);
                if (cmp < 0)
                    p = p.left;
                else if (cmp > 0)
                    p = p.right;
                else
                    return p;
            }
        }
        return null;
    }

由于您的值比较器的compare方法中的键存在比较,因此该条目将为null,因此值也将为null,因为map.get实现为

 public V get(Object key) {
        Entry<K,V> p = getEntry(key);
        return (p==null ? null : p.value);
    }