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 Organizing Data Interfaces

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

how our compare to method works

okay wo in the video on Interfaces we have Craig explaining in painfull detail compare method. as I understood this method can be inherited from Object class. In treet.java we cast our treet as object and then compare it to treet to see if its the same (WHY? Q.Q) I do get that it compares to the reference... so does the compiler compares our object (casted as Treet to the original treet?

 @Override
  public int compareTo(Object obj){
    Treet other = (Treet) obj;
      if (equals(other)){
        return 0;
      }

Then there is this bit of code :

int dateCmp = mCreationDate.compareTo(other.mCreationDate);
      if(dateCmp == 0){
        return mDescription.compareTo(other.mDescription);
      }
    return dateCmp;

The way I understood it, dateCmp holds the value that compare action returns for date values (-1, 0, 1) Then we check if dateCmp equals to zero and if it does it returns result of comparing DESCRIPTIONS (why what Im lost).

Thanks for your help in advance.

Lukas Baumgartner
Lukas Baumgartner
14,817 Points

I think that the equals() method checks if this 'other' Treet is equal to any other Treet Object. Have a look yourself: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object).

The second snipped you posted checks in the first if(), if the date of the two treets are the same. Which is very unlikely as Craig said, but better safe than sorry I guess. Now, if the unlikely event happens, that both dates are exactly the same, we check if the descriptions of the treets are identical.

If the dates are different. We return the dateCmp, which, as you said, compares the two dates and returns either a -1 if the other.mCreationDate is 'bigger' or a 1 if it is 'smaller'.

Hope it helps and i explained it correctly. :)

3 Answers

williamsandall
williamsandall
3,962 Points

Regarding your second question, as you have understood, if the dates are equal, and then the descriptions are equal, we assume the posts are the same so zero is returned.

If the descriptions are not the same, the compareTo() method of class String will return 1 or -1 depending on which of the two treet descriptions is "lexicographically greater" see String Comparator documentation https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)

it returns result of comparing DESCRIPTIONS (why what Im lost).

Whether 1 or -1 is returned is arbitrary, it doesn't really matter. But we don't want our 'Comparator' (our compareTo method) to return a zero because that would imply they are the same tweet, which they are not. Reasons are mentioned briefly by Craig and covered in the Comparable interface documents - it can basically result in strange behavior.

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

Thanks for your time, now I get better understanding!

Just to make sure I have decided to re-visit the course and do it again so it all sync in!

williamsandall
williamsandall
3,962 Points

As shown at the start of the video, the Comparable / compareTo() documentation shows it's usage as

x.compareTo(y)

In the case of the first code block you pasted, x is the object instance (hidden) which has 'called the method' compareTo, and y is the object instance which has been 'passed' to it (Object obj). The reason it is confusing is because nowhere in that code can we see which 'x' is calling the code.

if  (equals(other))

// is implicitly doing:

if (this.equals(other))

So we're definitely not comparing the same Object to itself cast to a Treet - we're comparing seperate Treet objects.. but which ones?

We call "Array.sort(treets)" in our Example.java class (see last video). The Array sort method, as explained on the Arrays documentation, has several overloaded methods, but in this case, because we are passing it a single Object Array it uses:

sort(Object[] a)
/*
"Sorts the specified array of objects into ascending order, according to the ***natural ordering** of its elements."
*/

Now for simplicity, we haven't delved into the exact nature of the sorting algorithm, but within the sort method, it will be using this natural order (Comparable interface i.e. compareTo) we created to repeatedly compare the different objects in the array.

Our compareTo() code is important because it tells the sort method (and any other method that might require Comparable) how exactly it should compare the components.

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

Kinda helped, gona finish this course and repeat it, maybe it will help with more knowledge in my head :D