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

edmond habimana
PLUS
edmond habimana
Courses Plus Student 8,352 Points

I am so confuse to the point where my head is about to explode.

Ok, so this piece of code is throwing me off.

    if(equals(other)){
      return 0;
    }

I read in the forum where people say we are comparing Treet to Treet class but even then i am still confuse, when we are trying to find out if they are equal are we looking at fields or the objects treet vs secondTreet.

Thanks.

5 Answers

Hi! equals() is a method of the Object class, and every type inherits it, including our Treet class (we can override the default implementation of course). So equals() is readily usable by our Treet instances (without having to be defined in the Treet class), and equals(other) is equivalent to this.equals(other). Inside the class definition, we do not need to use this explicitely if no confusion arises without it.

The equals() method compares the object on which it was called with the object passed in as argument. So, we are comparing this particular instance (a Treet object) to another object (other). The variable other was originally the parameter in our compareTo() method (in which we have this conditional block you've highlighted), so anything we pass in as argument to compareTo() will become the argument of our equals() method (and it will compare this Treet instance with that particular object).

We implemented the compareTo() method (and by doing so, the Comparable interface) in the Treet class so that later we could use the Arrays.sort() method on treets (which is an array of Treet objects containing - at the moment - Treet and secondTreet). Arrays.sort() orders the elements of the array passed in by calling the elements' compareTo() method (which in turn - at least in our implementation in the Treet class - calls the equals() method to check first if the two objects are equal before checking the date, description and author), so it will eventually call equals() on Treet to compare it to secondTreet.

I put the whole code here for others reading this post for reference:

public class Treet implements Comparable<Treet> {
...    

    @Override
    public int compareTo(Treet other) {
        if (equals(other)) {  // same as this.equals(other)
            return 0;
        }

        int dateCmp = creationDate.compareTo(other.creationDate);
        int descriptionCmp = description.compareTo(other.description);
        int authorCmp = author.compareTo(other.author);

        if (dateCmp == 0) {
            if (descriptionCmp == 0) {
                return authorCmp;
            }
            return descriptionCmp;
        }
        return dateCmp;
    }
...
}

Btw. the final part can be refactored, much nicer this way:

        if (dateCmp != 0) return dateCmp;
        if (descriptionCmp != 0) return descriptionCmp;
        return authorCmp;

(Note: I've combined my previous responses in this updated answer.)

Could you explain this part of the code to me? How does putting "other" in front of one mCreationDate compare the two different creation dates? How does the computer differentiate between the two different mCreationDates or mAuthors or mDescription? Please help.

int dateCmp = creationDate.compareTo(other.creationDate);
int descriptionCmp = description.compareTo(other.description);
int authorCmp = author.compareTo(other.author);

Garvit Kashyap

creationDate.compareTo(other.creationDate) is the same as this.creationDate.compareTo(other.creationDate). Inside the class definition, we do not need to use this explicitely if no confusion arises without it. this refers to the particular class instance (in this case a Treet object) on which we will call the compareTo() method. The other object is an object we should pass in as argument to the compareTo() method of the Treet class, it is given as a parameter in the method signature.

Note: kinda obvious, but it's worth pointing out that the compareTo() methods in these particular lines are not the same as the "outer" method we're talking about right now (the one implemented for the Treet class), i.e. we're not referring to the method in some recursive way, since these are not Treet objects. The Treet class's compareTo() method calls the attribute's own compareTo() implementation on each of them, to compare these different attributes of the two Treet objects in some specific order, and based on this, return us a comparison value for the whole Treet object (creationDate is a Date type, description & author are String types, so on the first line when compareTo() is called on the creationDate attribute it is the version implemented for the Date type, on the second and third line, when we're calling it on the description and author attributes [which are strings] it is the compareTo() method defined in the String type).

Thanks so much!

Douglas North
Douglas North
10,884 Points

I refused to continue the course till i got this, this discussion was very helpful, thanks guys.

Glad to hear it! Craig is awesome, but this course definitely needs an overhauling.

edmond habimana
PLUS
edmond habimana
Courses Plus Student 8,352 Points

When you say "we are comparing this particular instance (a Treet object) to another object (other)" do you mean comparing treet to secondTreet.

Again thanks for the help.

Edit: I removed my answer to this question and updated the first one, everything is there in one place.

hey yea, thanks man, i was the same as douglas and this was what was throwing me for a loop