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

Quinton Rivera
Quinton Rivera
5,177 Points

10:20 in video can't understand the logic behind the equals(other)) section of the compare to method code

Lost, cant understand the compareTo method inside the treet class, what object are we comparing the other to? i just see the name of the method then the other as the parameter

4 Answers

shelman, I'm not sure Simon answered your question. If not, I'll try from a different perspective.

Here's Craig's code:

  public int compareTo(Object obj) {
   Tweet other = (Tweet) obj;
   if (equals(other)) {
      return 0;
   }
   int dateCmp = mCreationDate.compareTo(other.mCreationDate);
      if (dateCmp == 0) {
         return mDescription.compareTo(other.mDescription);
      }
      return dateCmp; 
  }

This method takes one parameter, an obj of type Object. But it must have a Tweet for the equals comparison in the third line, so it first casts the obj to a Tweet. (Note, in passing, that if obj is not a Tweet this cast will fail, and there's no code to handle the error. )

Then it compares the incoming Tweet (other) with the current one, the one the method was called on.

Note that the line of code could have been written this way:

if (this.equals(other)) {

Here the this makes it explicit that it is the current object that is being compared to the other object. If you are not familiar with the keyword this think of it, here, as the pronoun I, and what the if statement is asking is if I (the current Tweet object) am equal to the incoming Tweet object. If I am, then return 0.

If I am not equal to other, then the comparison becomes one between our creation dates (using the compareTo method of the Date class), and of those are equal, then it becomes one between our descriptions (using the compareTo method of the String class).

Hope this helps.

Simon Coates
Simon Coates
28,694 Points

it sounded like shelman was confused about the .equals bit and the why the other object was being fed in . So i glossed over the subsequent conditionals. You explained them better than i was likely to have anyway. I also didn't do a great job of explaining why being comparable matters.

Thank you sir! I was getting rather frustrated with this one.

Ciaran McNally
Ciaran McNally
7,052 Points

Great explanation. Using the this. keyword makes many of these concepts much easier to digest.

Simon, I think you covered the basics, viz., that you must override compareTo() if you want your class to implement the Comparable interface -- so you can sort the objects of the class in some way.

The class in the video is implementing Comparable, so there must be a compareTo() method or the compiler will complain, and the parameter of compareTo() must be an Object.

If it implemented Comparable<Tweet>, then the parameter would have to be a Tweet obj, thereby avoiding the problem of obj not being a Tweet (and not castable).

You also hinted at the reason it uses equals() rather than ==, viz., because it's comparing two objects, rather than two primitives.

Simon Coates
Simon Coates
28,694 Points

equals is defined on object and compares that the object is literally the same object (the same spot in memory). If not, it has to do a more fine grained comparison. compareTo is for comparison of an object against a passed in object. That's what it's for. That's what it does. An example class is Date , where there is a compareTo, and the class implements the Comparable Interface (that i think requires the compareTo).

I understand that an incoming object is being compared to the object that is currently in memory. However, if the mCreationDate and the mDescription date are the same, why use equals? It must mean it is the same object right? it's doing nearly the same job? Or am I not understanding this fully?