patternjavaMinor
Alternate way for comparison call back function?
Viewed 0 times
wayfunctioncomparisoncallbackforalternate
Problem
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:
I have the usual get/set functions. This item go into a tree, whose definition is:
I omitted all the other functions to help with readability. I am creating the tree in the following way:
Are there any other options/ideas for the call back?
public class myTreeItem {
private T item;
private myTreeItem left;
private myTreeItem right;
private myTreeItem parent;
}I have the usual get/set functions. This item go into a tree, whose definition is:
public class myTree {
myTreeItem root;
protected comparableCallBack callback; // call back function for comparisons, to be provided by the creator of the tree
public interface comparableCallBack {
int onCompare(myTreeItem item1, myTreeItem 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 tree1 = new myTree();
tree1.setCallback(new comparableCallBack() {
public int onCompare(myTreeItem item1,
myTreeItem item2) {
// Do the actual comparison here.
if (item1.getItem().intValue() < 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?
Solution
Comparator
There is no need for your interface.... what's wrong with
The reason you appear to have it is because you need to compare the
So, your code has:
but it should instead have:
and then, your library should have it's own private Comparator that looks something like:
Code Style
Read the Java Code-style guidelines.
Java class names should start with a capital letter.
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() {
public int onCompare(myTreeItem item1,
myTreeItem item2) {
.......
}
});but it should instead have:
tree1.setCallback(new Comparator() {
public int compare(Integer item1, Integer item2) {
.......
}
});and then, your library should have it's own private Comparator that looks something like:
class TreeComparator implements Comparator> {
private final Comparator delegate;
TreeComparator(Comparator delegate) {
this.delegate = delegate;
}
public int compare(myTreeItem a, myTreeItem 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.
Code Snippets
tree1.setCallback(new comparableCallBack<Integer>() {
public int onCompare(myTreeItem<Integer> item1,
myTreeItem<Integer> item2) {
.......
}
});tree1.setCallback(new Comparator<Integer>() {
public int compare(Integer item1, Integer item2) {
.......
}
});class TreeComparator <T> implements Comparator<myTreeItem<T>> {
private final Comparator<T> delegate;
TreeComparator(Comparator<T> delegate) {
this.delegate = delegate;
}
public int compare(myTreeItem<T> a, myTreeItem<T> b) {
return delegate.compare(a.getItem(), b.getItem());
}
}Context
StackExchange Code Review Q#44913, answer score: 3
Revisions (0)
No revisions yet.