我们如何在TreeMap中使用异构键

时间:2016-01-04 12:37:16

标签: java

在执行下面提到的代码时我正在

WITH CUSTS AS (SELECT 1000 AS CUST_NO, TO_DATE('01-JAN-2015', 'DD-MON-YYYY') AS START_DATE, TO_DATE('31-DEC-2015', 'DD-MON-YYYY') AS END_DATE FROM DUAL UNION ALL
               SELECT 1001 AS CUST_NO, TO_DATE('02-JAN-2015', 'DD-MON-YYYY') AS START_DATE, TO_DATE('31-DEC-2016', 'DD-MON-YYYY') AS END_DATE FROM DUAL UNION ALL
               SELECT 1002 AS CUST_NO, TO_DATE('02-JAN-2015', 'DD-MON-YYYY') AS START_DATE, TO_DATE('31-MAR-2015', 'DD-MON-YYYY') AS END_DATE FROM DUAL UNION ALL
               SELECT 1003 AS CUST_NO, TO_DATE('03-JAN-2015', 'DD-MON-YYYY') AS START_DATE, TO_DATE('31-AUG-2015', 'DD-MON-YYYY') AS END_DATE FROM DUAL ),
     DATES AS (SELECT DISTINCT TRUNC(TO_DATE('04-JAN-2015', 'DD-MON-YYYY') - DAYINCREMENT, 'DD') AS DT
                FROM (SELECT LEVEL-1 AS DAYINCREMENT
                        FROM DUAL
                        CONNECT BY LEVEL <= 30))
SELECT d.DT, COUNT(*)
  FROM CUSTS c
  CROSS JOIN DATES d
  WHERE d.DT BETWEEN c.START_DATE AND c.END_DATE
  GROUP BY d.DT
  ORDER BY DT DESC

代码:

Exception in thread "main" java.lang.ClassCastException: com.test.tree.T1 cannot be cast to com.test.tree.T2
    at com.test.tree.TestComparator.compare(TestTreeMap.java:59)
    at java.util.TreeMap.put(TreeMap.java:530)
    at com.test.tree.TestTreeMap.main(TestTreeMap.java:22)

3 个答案:

答案 0 :(得分:3)

您不能假设compare将始终收到T1个实例作为第一个参数,T2个实例作为第二个参数。它可能会收到两个T1实例或两个T2实例,或两者的实例,但不会按照您预期的顺序。

您必须先测试o1o2的类型,然后再将其转换为T1T2

@Override
public int compare(Object o1, Object o2) {
    if (o1 instanceof T1) {
        if (o2 instanceof T2) {
            T1 t1=(T1)o1;
            T2 t2=(T2)o2;
            return t1.x-t2.y;
        } else {
           ...
        }
    } else {
        ...
    }
}

这假设您TreeMap的密钥只能是T1T2

答案 1 :(得分:1)

到目前为止,两个单独的条款没有意义,只需使用:

public static void main(String[] args) {

    TreeMap tree=new TreeMap(new TestComparator());
    T1 t1=new T1(10, 20);
    T1 t2=new T1(10, 21);
    tree.put(t1, 23);
    tree.put(t2, 24);
}

或继承课程T2中的课程T1并使用:

class TestComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        System.out.println("hi im called");
        T1 t1=(T1)o1;
        T1 t2=(T1)o2;
        return t1.x-t2.y;
    }

}

或者声明一些必须由T1T2类实现的正式接口,并且无论参数是什么,都允许处理比较错误:

interface Comparable {
    int comparisonToken();
}

class T1 implements Comparable {
    int comparisonToken() {
        return this.x;
    }
    ...
}

class T2 implements Comparable {
    int comparisonToken() {
        return this.y;
    }
    ...
}

class TestComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        System.out.println("hi im called");
        Comparable t1= (Comparable)o1;
        Comparable t2= (Comparable)o2;
        return t1.comparisonToken() - t2.comparisonToken();
    }

}

答案 2 :(得分:0)

使用以下代码:

 @Override
    public int compare(Object o1, Object o2) {
        System.out.println("hi im called");
        T1 t1 = null;
        T2 t2 = null;
        if(o1 instanceof T1){
            t1=(T1)o1;
        } else if(o1 instanceof T2){
            t2 = (T2)o1;
        }
        if(o2 instanceof T2){
            t2=(T2)o2;
            return t1.x-t2.y;
        } else{
            t1 = (T1)o2;
            return t1.x-t1.y;

        }

        }