Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparable<T> - must override compareTo()???

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ladies/Gents,

OK, so, generics are becoming slowly clearer to me, but still having a few troubles. I implemented a function, min(), which should return the smallest element in an array. I think my function will work, but can't be sure, because now I can't compile. The specific error I'm getting is that I'm not overriding compareTo() in this class. Now, I didn't get this error until I coded the min() function, so I'm sure it's due to min()'s use of Comparable<T>. I, however, don't see why I should have to override compareTo() for the class, since I'm only using Comparable<T> with the elements of the list, which is an instance variable of this class (comes from the parent class). Would the best thing actually be to code a compareTo() for this class, even though it's kind of unnecessary for my purposes? Is my design inherently flawed, and if so, where did I go wrong? Thanks in advance!

*Update - I put in a phony compareTo() method, and the class still works fine, and the min() function works. Why did I have to put that method in? Makes me think I went about it the wrong way. Any advice/constructive criticism is always much appreciated!

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here's the function.



I don't know what type "list" is, because it's declared in the base class. But in this function you assign an element of list to a variable of type T, and also to a variable of type Comparable<T>. So the elements of list have to have both those types -- only possible if T implements Comparable<T>. I'm not at all clear why you said your class didn't need to implement Comparable, or why you say the method isn't being called - you're calling it right there. Maybe I'm missing something? I think this hinges on the declaration of "list".
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:
I don't know what type "list" is, because it's declared in the base class. But in this function you assign an element of list to a variable of type T, and also to a variable of type Comparable<T>. So the elements of list have to have both those types -- only possible if T implements Comparable<T>. I'm not at all clear why you said your class didn't need to implement Comparable, or why you say the method isn't being called - you're calling it right there. Maybe I'm missing something? I think this hinges on the declaration of "list".


list is an array of T that was instantiated in ArrayListClass, this class' parent class, like so:

, and then, of course, super(size) is called in this class to instantiate. I guess I did miss that I assigned both T and Comparable<T> to these elements. How should I have written this function, then, to avoid this problem? What I mean by the necessity of the compareTo() is: what good will a compareTo() function do for the class UnorderedArrayList when it is the elements of T[], which are whatever objects we instantiate them as (String, Integer, Person, Animal...) that are really being compared, and not UnorderdArrayLists? You know, I really feel like once I take a step forward with generics, I take five backward.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing your class declaration as follows:

This means that T will be able to compare itself to T; the "? super T" means that T can also be comparable to super interfaces. For example, ScheduledFuture extends Delay, which again extends Comparable<Delay>. So ScheduledFuture is not a Comparable<ScheduledFuture>, but a Comparable<Delay>, and the "? super T" will make sure that ScheduledFuture will also be a match for T.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Try changing your class declaration as follows:

This means that T will be able to compare itself to T; the "? super T" means that T can also be comparable to super interfaces. For example, ScheduledFuture extends Delay, which again extends Comparable<Delay>. So ScheduledFuture is not a Comparable<ScheduledFuture>, but a Comparable<Delay>, and the "? super T" will make sure that ScheduledFuture will also be a match for T.



Well, I'll be darned, now I don't have to cast to Comparable<T> any more! OK, so, just to make sure I understand this: 'T extends Comparable' will make sure that any T must have its own version of compareTo(). The '? super T' is a wildcard with a lower bound, which means that it will be comparable to anything that is a superclass of T, or even another T itself. This, however, would mean that I can't compare any T against a subclass of that T. Is that correct?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass any variable to list[0].compareTo as long as it IS-A T - so that's instances of T or any subclass / implementations. That's because the actual method is compareTo(T), or compareTo(X) (with X being the direct superclass of T), or perhaps even compareTo(Object). But in the end, they all can take anything that IS-A T.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You can pass any variable to list[0].compareTo as long as it IS-A T - so that's instances of T or any subclass / implementations. That's because the actual method is compareTo(T), or compareTo(X) (with X being the direct superclass of T), or perhaps even compareTo(Object). But in the end, they all can take anything that IS-A T.


OK, thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic