I am writing a generic collection based on a binary tree model.
class MyTree <T extends Comparable<T>> extends AbstractCollection<T>{...}
The underlying Node<T>
class (among others) contains the following methods:
public Node<T> getLeft() // left Node
public Node<T> getRight() // right Node
public T getValue() // value stored in the Node
I want to override the method boolean contains(Object o)
of the interface AbstractCollection<T>
to have the possibility to check for Object
's of a type other than T
.
For the tree traversal in O(log n) the generic type T
must implement Comparable<T>
, so it has the method compareTo(T t)
.
My code:
@Override
public boolean contains(Object o){
T t = (T) o; // produces warning (see below)
BSNode<T> currentNode = this.root;
while(currentNode != null){
if(currentNode.getValue().equals(o)) {return true;}
if(currentNode.getValue().compareTo(t) < 0) {currentNode = currentNode.getRight();}
if(currentNode.getValue().compareTo(t) > 0) {currentNode = currentNode.getLeft();}
}
return false;
}
The problem is that I can not just cast Object o
to T t
for using compareTo(T t)
. Technically the Object
's are castable to T
, but as T
is a generic type, I get this warning:
warning: [unchecked] unchecked cast
T t = (T) o;
^
required: T
found: Object
where T is a type-variable:
T extends Comparable<T> declared in class MyTree
Can someone either
- confirm that I can safely ignore the warning using
@SuppressWarnings("unchecked")
, - suggest how I can safely cast
Object
toT
, - explain why neither of the points above can be satisfied so I can stop thinking about how to make this work?
Thanks a lot!