I want to implement a generic type binary search tree. The declarations are as follow:
public BTNode<T> {}
public class BinaryTree<T extends Comparable<T>> {}
public class BinarySearchTree <T extends Comparable<T>> extends BinaryTree<T> {}
Now I have written a class called Entry and want to store the instances of this class in the BinarySearchTree.
public class Entry implements Comparable{
private String firstName, lastName, address, phoneNumber;
public Entry(String fName, String lName, String address, String phoneNum) {
this.firstName = fName;
this.lastName = lName;
this.address = address;
this.phoneNumber = phoneNum;
}
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
}
But when I declare BinarySearchTree<Entry> bst, there is always an compile error saying:
"Bound mismatch : The type Entry is not a valid substitute for the bounded parameter > of the type BinarySearchTree"
I am still quite new to the generic type in Java. Can anybody help me solve the problem? Thanks
<T extends Comparable<T>>this line in your interface should be<T implements Comparable<T>>extendsis used in generics regardless of classes or interfaces. If you want to say T thatextends Foo implements Bar, then you use<T extends Foo & Bar><T extends Comparable<? super T>>