Evlauating object inequalities iwth generics?
If I have a class written that I want to be able to accept generic Objects types, how would I compare those objects?
I can test to see if the objects are equal, but I need inequalities for searching and sorting purposes. I am hoping there is a way to do this without have type casts all over the place. I basically want to be able to use the functionality of compareTo()
Any help would be greatly appreciated!
How far along are you with this? Do you have a non-generic solution already, or are you starting from scratch?Do you want to use natural order (Comparable) or external (Comparator)?Thanks,Alex.
Well I have a non-generic setup right now. It's a binary search tree. I've done this in C but not Java.
I want my tree functions to be able to accept multiple types (integer, string etc) and be able to sort them.
And I am not sure what you mean by natural order?
BTW I think I know the basics of what I will need to do...
public class myclass<TYPE> {
public foo(TYPE foo) {
}
}
But I still need to be able to compare (less than/greater than) for searching/sorting purposes.
Thanks for the help!
If you take a look at java.lang.String and java.lang.Integer, you'll see that they both implement Comparable, and hence the compareTo method. When I say "natural order", I just mean the order that this method imposes, "a" < "b" < "c" and 1 < 2 < 3 etc.
I don't know if there's a reason you need to implement this from scratch or not. Have you seen java.util.SortedSet and java.util.TreeSet? They do something similar to what you are describing.
Anyway, see if this is any help: http://java.sun.com/j2se/1.5.0/docs/api/java/util/SortedSet.html
Well, I wanted to see what I could do on my own. The whole point of the program I am working on is to practice with some ADTs in Java. I have been exclusively coding in C just about all summer so I am not right on with all the features.
Well, after brushing up on interfaces and looked at SortedSet I think I have a general idea of what I would have to do.
But I don't see how implementing that interface will allow me to evaluate inequalities....
Would I just be taking subsets (for instance) that are less than a given value, but that the set contains only two elements, which would be two node values from a tree.
Again, thanks for the replies.
Ok, I just cast all my instances of the objects (of type object) to Comparable.
ie.((Comparable)obj).compareTo((Comparable)obj2)
It seems to work for the most part, except this doesnt seem to be a good solution because it doesnt work correctly with numbers ('322' would be sorted before '39'). Obviously. Again, I am back at square 1.
No, if you are casting things (under Java 5), you are generally doing things wrong.
Consider this piece of code:public class MyClass<T extends Comparable><T>> {
public int compare(T t1, T t2) {
return t1.compareTo(t2);
}
}
It invokes the comparison-function without casting and won't compile with a type that isn't naturally self-comparable.
M.
> ((Comparable)obj).compareTo((Comparable)obj2)
> It seems to work for the most part, except this doesnt seem
> to be a good solution because it doesnt work correctly with
> numbers ('322' would be sorted before '39').
Well, that depends on the type of objects that obj and obj2 are. If there are instances of java.lang.String, then "322" < "39", but if they are instances of java.lang.Integer (or some other numeric type) then they will compare as you expect. You can impose what ever (natural) ordering you like on classes that you write. String and Integer just happen to come with compareTo methods that do the 'sensible' thing.
> Obviously. Again, I am back at square 1.
I wouldn't say so. I don't know what other posters think, but I suggest trying to get something basic working without generics (all the Comparable<T> stuff) and just use casting to start with (as you've shown). Once you have that, take a look at Malvolio said about eliminating the casts by using generics.
Alex.
