Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Interfaces

Cody Te Awa
Cody Te Awa
8,820 Points

Not returning '1' or '-1' in the compareTo method?

How come when re-implementing the toCompare method, we only return cases of which they are equal? The original code returns -1 if it is less that and 1 if it is greater than. So how does java know if it doesn't know how to compare treats that just because they are not equal that one treat is greater or less than the other treet? (reference purposes: I am only talking about the first if block where he is using the equals method because i understand that the second if block where he compares dates is already implemented by the Interface/method)

Hopefully this question made sense!

1 Answer

Allan Clark
Allan Clark
10,810 Points

Yes the convention for compareTo() method is to return -1 for less, 0 for equal, and 1 for greater. That is the same convention for the Override of compareTo(). So compareTo() has 3 possible return values. The first if statement is getting the easiest one out of the way, when comparing Treets that are equal return 0. Java knows what it means for two Treets to be equal because we have defined the equals() method.

Past that if block we can assume that the two Treets are not equal (otherwise the return would have taken us out of this method). We are comparing Treets on a basis of the mCreationDate.

public int compareTo(Object obj) {
   Treet other = (Treet) obj;
   if(equals(other)) {
      return 0;
      //if both Treets are equal as defined in the equals() method
      //return 0
   }
   int dateCmp = mCreationDate.compareTo(other.mCreationDate);
   // The Comparable interface guarantees that using compareTo() 
   // will return -1,0, or 1. So dateCmp will have one of those 3 possibilities 
   if(dateCmp == 0) {
      //The compareTo() on the dates could return 0
      //meaning both were created on the same date.
      //Since the dates are the same we use another metric to compare,
      //namely the description.
      return mDescription.compareTo(other.mDescription);
      //we don't have to worry about if this is 0 
      //because if both the date and desc are the same it would 
      //be caught by the equals() method in the first if block
   }
   //At this point in the code we know the Treets are not equal
   //and they have different dates. Date is what we want
   //to compare so we just return that value.
   return dateCmp;
}

So in short Java knows how to compare Treets because you tell it how to (i.e. the mCreationDate).

Hope this helps, let me know if i can clear anything up.