简体   繁体   中英

Can't TreeSet check for comparator objects at compile-time?

When creating an instance of the class TreeSet in Java with the default constructor (no arguments) and adding two objects of classes that don't implement the Comparator interface, the object will throw a run-time exception. Could this check be implemented at compile time?

I have tried the following code:

Dummy.java

public class Dummy {
}

TreeSetTest.java

import java.util.TreeSet;

public class TreeSetTest {

    public static void main(String[] argv) {
        TreeSet<Dummy> treeSet = new TreeSet<>();
        treeSet.add(new Dummy());
        treeSet.add(new Dummy());
    }
}

The compiler should complain when creating the TreeSet as it doesn't implement Comparable.

If the TreeSet() constructor were not public, you could have checked this at compile time, with a factory method like so:

public static <E extends Comparable<? super E>> TreeSet<E> create() {
  return new TreeSet<>();
}

You can only invoke this with a type parameter which is naturally comparable:

TreeSet<String> a = TreeSet.create(); // ok
TreeSet<Object> b = TreeSet.create(); // error

But the constructor is public; and there is no language mechanism in Java to allow you to restrict the type parameters used to invoke a constructor.

This constructor had to be public because it existed before generics, and so removing it would have violated backwards compatibility.

You could write your own function so that it needs to implement Comparable to compile:

static <E extends Comparable> boolean addWithCheck(TreeSet<E> treeSet, E e) {
    return treeSet.add(e);
}

And then call it like this:

TreeSet<Dummy> treeSet = new TreeSet<>();
addWithCheck(treeSet, new Dummy());

No, the compiler should not complain about that.

Lets assume that you also have a subclass of Dummy implementing Comparable :

public class NotSoDummy extends Dummy implements Comparable {
    @Override
    public int compareTo(Object o) {
        ...
    }
}

Instances of that class should be added to the TreeSet without problems. The Compiler cannot check all code (actual, future) to check if there is or not an Dummy extension that eventually can be added to the TreeSet .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM