I am trying to create a hybrid list that uses Binary Search Trees for various functions. The class tries to implement List< E >
. To index, these trees use comparison to decide whether data should be placed in the left or right child of the parent. This is done using a Comparator
variable set by the Constructor:
private Comparator<E> comparator; // method for ordering the tree
This ensures that type E
will be comparable in some way. The problem however is in how the List Interface declared its abstract functions. For example:
public boolean contains(Object o);
Normally, I would assume that the parameter is comparable and then use a recursive lookup method on the tree, but because the parameter is Object, I cannot make that assumption.
I have tried to ensure that it is of type E
by doing the following:
if (o instanceof E) {}
but it does not work and gives me this suggestion:
Cannot perform instanceof check against type parameter E.
Use its erasure Object instead since further generic type information will be erased at runtime
Any ideas on how to solve this?
Edit:
This is how the class is instantiated:
TreeCollection<Integer> c = new TreeCollection<>(
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});