Alternate way for comparison call back function? I'm doing a programming practice problem and was wondering if someone can suggest a better to create/setup the call back for the comparison function. I have the following classes:

public class myTreeItem&lt;T&gt; {
    private T item;
    private myTreeItem&lt;T&gt; left;
    private myTreeItem&lt;T&gt; right;
    private myTreeItem&lt;T&gt; parent;
}


I have the usual get/set functions. This item go into a tree, whose definition is:

public class myTree&lt;T&gt; {
    myTreeItem&lt;T&gt; root;
    protected comparableCallBack&lt;T&gt; callback; // call back function for comparisons, to be provided by the creator of the tree

    public interface comparableCallBack&lt;T&gt; {
        int onCompare(myTreeItem&lt;T&gt; item1, myTreeItem&lt;T&gt; item2);
    }
}


I omitted all the other functions to help with readability. I am creating the tree in the following way:

    // create a regular tree
    myTree&lt;Integer&gt; tree1 = new myTree&lt;Integer&gt;();
    tree1.setCallback(new comparableCallBack&lt;Integer&gt;() {
        public int onCompare(myTreeItem&lt;Integer&gt; item1,
                myTreeItem&lt;Integer&gt; item2) {
            // Do the actual comparison here.
            if (item1.getItem().intValue() &lt; item2.getItem().intValue())
                return -1;
            else if (item1.getItem().intValue() == item2.getItem().intValue())
                return 0;
            else
                return 1;
            }
    });


Are there any other options/ideas for the call back?
Comparator

There is no need for your interface.... what's wrong with Comparator? Why do you need to have your own?

The reason you appear to have it is because you need to compare the myTreeItem against another myTreeItem and the generic type is not right for you, but you can solve it easily with wrapping the Comparator in your myTree class when you need to....

So, your code has:


tree1.setCallback(new comparableCallBack&lt;Integer&gt;() {
        public int onCompare(myTreeItem&lt;Integer&gt; item1,
                myTreeItem&lt;Integer&gt; item2) {
            .......
        }
    });



but it should instead have:

tree1.setCallback(new Comparator&lt;Integer&gt;() {
        public int compare(Integer item1, Integer item2) {
            .......
        }
    });


and then, your library should have it's own private Comparator that looks something like:

class TreeComparator &lt;T&gt; implements Comparator&lt;myTreeItem&lt;T&gt;&gt; {

     private final Comparator&lt;T&gt; delegate;
     TreeComparator(Comparator&lt;T&gt; delegate) {
         this.delegate = delegate;
     }
     public int compare(myTreeItem&lt;T&gt; a, myTreeItem&lt;T&gt; b) {
         return delegate.compare(a.getItem(), b.getItem());
     }
}


Code Style

Read the Java Code-style guidelines.

Java class names should start with a capital letter.