English 中文(简体)
Java: 收集信息
原标题:Java: about Collections ReverseComparator implementation

执行如下(jdk1.6.0_31):

private static class ReverseComparator<T>
implements Comparator<Comparable<Object>>, Serializable {

// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 7207038068494060240L;

    public int compare(Comparable<Object> c1, Comparable<Object> c2) {
        return c2.compareTo(c1);
    }

    private Object readResolve() { return reverseOrder(); }
}

为什么要执行:

private static class ReverseComparator<T extends Comparable<T>> 
implements Comparator<T>, Serializable {

// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 7207038068494060240L;

    public int compare(T c1, T c2){
        return c2.compareTo(c1);
    }
    ...
}

它是否只是风格,还是有更深的理由?

EDIT:显示的来源代码来自Sun/Oracle jdk(jdk1.6.0_31)。

最佳回答

我认为,这都与使反转录委员会成为单一吨物体的意图有关。 由于单一州的情况必须在静态的情况下加以界定,因此在使用任何通用类型方面没有意义。

static final ReverseComparator REVERSE_ORDER = new ReverseComparator();

该法典产生了一种原始的类型警告。

因此,只能用于这一问题的反逆反勒索行为的实施本可以像你所建议的那样,也可以是执行。 也许他们选择了目前的执行,因为阅读比较容易,而且他们认为,如果只是为了这一简单的目的使用,就不需要进一步普及。

追捕 Java公司对你的执行和Oracle公司的执行产生同样的原始编码。

 public int compare(java.lang.Comparable, java.lang.Comparable
 public int compare(java.lang.Object, java.lang.Object);

归根结底,如果参照器通过<代码>逆向型Order()方法的收款类别公开接口,则不可能避免这种 cast落和不受控制的警告。 但我们大家都确信,无论所涉类型如何,这都不会失败。

Bottom line, IMHO I think the only reason why it was implemented as it was has to do with code clarity, or with the desire of not complicating the things more than necessary if, anyways, the unchecked warning could not be prevented. But hey, this wouldn t be the first time I am wrong ;-)

问题回答

进行公正的猜测,但储存在静态现场

static final ReverseComparator REVERSE_ORDER
            = new ReverseComparator();

因此,你的版本将产生一种原始类型的警告。

I m looking at Oracle 1.6.0_26, but I see the same code. As far as I can tell, those are functionally equivalent. You could also potentially write it like this:

private static class ReverseComparator<T> implements Comparator<Comparable<T>>, Serializable {

    // use serialVersionUID from JDK 1.2.2 for interoperability
    private static final long serialVersionUID = 7207038068494060240L;

    public int compare( Comparable<T> c1, Comparable<T> c2 ) {
        return c2.compareTo( (T) c1 );
    }

    private Object readResolve() {
        return reverseOrder();
    }
}

我只想到为什么使用<代码>Comparable<Object>的类别(或Comparable(或Comparator/code>)应当遵守<代码>equis( Contracting, usecode>Object。 从理论上讲,这强调了这一联系。 除此之外,我也想不到原因。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...