Issues with comparing two generic objects
My project is a Phonebook that is using BSTree<E>. Each node of the tree is BTNode<E>. In the main class, I replace E with Pair class, which has (String name, String number), when I define the nodes.
I have the following comparator class to compare between 2 E types:
and I use it in BSTree<E>.
public class BTNodeComparator<E extends Comparable<E>> implements Comparator<E>
public int compare(final E a, final E b)
Also, I define compareTo() method in Pair class:
Now, when I run the program and it comes to the comparator, it gives me errors at the comparator because now it compares between two Pairs
public int compareTo(Pair pair)
How can I solve this issue? What I want is to compare between the names of the two pairs ?
Sorry for my bad explanation. My English is weak.
This error what I got after insert the second node (Pair) in BST:
at Pair.compareTo(Pair.java:36) // @ return name.compareTo(pair.getName());
at Pair.compareTo(Pair.java:2) // @ public class Pair implements Comparable<Pair>
at BTNodeComparator.compare(BTNodeComparator.java:24) // @ return (a.compareTo(b));
at BTNodeComparator.compare(BTNodeComparator.java:20) // @ public class BTNodeComparator<E extends Comparable<E>> implements Comparator<E>
BTW, I decleared BTNodeComparator in BSTree as follows:
Also, I attached the .java files
protected Comparator<E> c = new BTNodeComparator();
if (c.compare(target, cursor.getElement()) < 0) cursor = cursor.getLeft();
Thanks in advance for any help :)