使TreeMap Comparator容忍null [英] Make TreeMap Comparator tolerate null

查看:151
本文介绍了使TreeMap Comparator容忍null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个自定义的Valuecomparator按其值对TreeMap进行排序。但是在搜索TreeMap是否具有某个键时,它不会容忍nullpointexception。如何修改比较器以处理nullpoint?

This customized Valuecomparator sorts the TreeMap by it's value. But it doesn't tolerate nullpointexception when searching whether the TreeMap has a certain key. How do i modify the comparator to handle the nullpoint?

    import java.io.IOException;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.TreeMap;



    public class TestTreeMap {

        public static class ValueComparator<T> implements Comparator<Object> {

            Map<T, Double> base;
            public ValueComparator(Map<T, Double> base) {
                this.base = base;
            }

            @Override
            public int compare(Object a, Object b) {
                /*if (((Double) base.get(a) == null) || ((Double) base.get(b) == null)){
                    return -1;
                }   */      
                if ((Double) base.get(a) < (Double) base.get(b)) {
                    return 1;
                } else if ((Double) base.get(a) == (Double) base.get(b)) {
                    return 0;
                } else {
                    return -1;
                }
            }

        }

        public static void main(String[] args) throws IOException { 
            Map<String, Double> tm = new HashMap<String, Double>();
            tm.put("John Doe", new Double(3434.34)); 
            tm.put("Tom Smith", new Double(123.22)); 
            tm.put("Jane Baker", new Double(1378.00)); 
            tm.put("Todd Hall", new Double(99.22)); 
            tm.put("Ralph Smith", new Double(-19.08)); 

            ValueComparator<String> vc = new ValueComparator<String>(tm);
            TreeMap<String, Double> sortedTm = 
                    new TreeMap<String, Double>(vc);
            sortedTm.putAll(tm);

            System.out.println(sortedTm.keySet());
            System.out.println(sortedTm.containsKey("John Doe"));
            // The comparator doesn't tolerate null!!!
            System.out.println(!sortedTm.containsKey("Doe"));
        }


}


推荐答案

这不是火箭科学......

This is not rocket science ...

将其插入已注释掉的代码:

Insert this in the place of the commented out code:

if (a == null) {
    return b == null ? 0 : -1;
} else if (b == null) {
    return 1;
} else 

这款 null 作为比任何非空 Double 实例更小的值。

This treats null as a smaller value than any non-null Double instance.

您的版本不正确:

if ((a==null) || (b==null)) {return -1;}

这表示如果a为null或b为null,则a小于b 。

This says "if a is null or b is null then a is smaller than b".

导致虚假关系,如

null < 1.0  AND 1.0 < null

null < null

当set / map中有空值时,这种事情会导致树不变量中断,并导致不一致和不稳定的密钥排序......更糟糕的是。

This sort of thing causes the tree invariants to break when there are nulls in the set / map, and leads to inconsistent and unstable key ordering ... and worse.

有效比较的要求 方法在 javadocs 。数学版本是该方法必须定义 总订单 所有可能输入值的域。

The requirements for a valid compare method are set out in the javadocs. The mathematical version is that the method must define a total order over the domain of all possible input values.

这篇关于使TreeMap Comparator容忍null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆