I am looking for suggestions on how to handle some possible elements implementing comparable and some not.
I am currently trying to do it like this
class BinaryTree<E> {
private Comparator<E> comparator;
public <T extends Comparable<E>>() BinaryTree() {
comparator = (T a, T b) -> a.compareTo(b)
}
public BinaryTree(Comparator<E> comparator) {
this.comparator = comparator
}
}
//rest of the class (add, remove etc)
And then the comparator variable is used. I cant get it to compile tho.
Just check and see if a comparator has been provided. If so, use it. Otherwise,
presumethat the objects implement it. The downside is that if the objects don't implement it you will get a runtime exception instead of a compiler error.