Search code examples
javagenericscollectionsinterfacemarker-interfaces

What does self comparable interface do in Collections Class?


While practicing Reflection I came to know about SelfComparable Interface in Collections class

interface java.util.Collections$SelfComparable

What does this interface use for ?


Solution

  • It doesn't do anything. It is private so you can't import it.

    It is really a comment that the type is "SelfComparable" and is not actually used.

    Nothing implement this interface. The code which uses it relies on the fact it will be discarded at runtime.

    public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
        if (comp==null)
            return (T)max((Collection<SelfComparable>) (Collection) coll);
    

    could have been

    public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
        if (comp==null)
            return (T)max(/*SelfComparable*/ (Collection) coll);
    

    as it will be ignored at runtime.