Java Java Data Structures Organizing Data Interfaces

Chris Rubio
Chris Rubio
1,643 Points

Please explain the compareTo(obeject obj) method that craig is using.

Ok this java course has really came to a point where i am not comprehending anything of whats going on, if someone would be kind enough to explain the compareTo method that craig is using, why is he using it ? what is he trying to do with it ? please feel free to ellaborate as if talking to a 7 year old, i do not mind, the SIMPLER the better to be honest

Chris Rubio
Chris Rubio
1,643 Points

P.S not only the compareTo method , but i mean the entire method, the if staments being used , everything please.

Michele Eddy
Michele Eddy
948 Points

I'm struggling a bit with this concept as well but I'll try to explain my understanding. I finished the lesson today to sorry if I have an incorrect understanding

The idea of the compareTo in the interface Comparable is to compare two whole objects. In the example we aren't wanting to compare two whole objects rather we want to compare the time stamp(date) of objects. We @Override the compareTo method of the Comparable interface in order to define our own check on the objects(We compare private variables of the objects rather than the whole object). One key take away is that an @Override HAS to have the same parameters and "should" return the same values.

One confusing part of the method we created is the compareTo that is also used to compare the dates, even though we are using the method inside a method meant to @Override that method. From what I can tell the reason that this work is because the Date object also implements the Comparable interface so the mCreationDate.CompareTo(other.mCreationDate) is using a method defined in the class Date. Source:

In the end Craig is trying to compare two private variables of the same object in order to determine which was posted first, which I assume might be used for a sorting the objects. To be honest I have no idea what the first if statement is trying to accomplish and I haven't been able to find documentation on (equals(obj)) : (

1 Answer

Oleksandr Prygara
Oleksandr Prygara
1,113 Points

Hi there,

What we are trying to do in this video is to SORT the treets or, in other words, put them in some order.

  • Question: what kind of order??? As the treets that we have created are the instances of the classes also created by us (we created the blueprint for this object), it is up to us to say how we should SORT them. The system has no clue how to SORT them... However, if we want to SORT some Strings, this object (I mean String) was created by the guys who developed the language itself, so they wrote the rule for SORT method in advance. Since they decided to SORT the Strings alphabetically, "ananas" will always be in front of "banana", though I prefer bananas.

-Question: how does the system sort the objects?? To SORT the objects, the system compares them with each other and defines which one goes first. Since the java creators decided to SORT the strings alphabetically (and not according to what I prefer to eat), ananas will always be before bananas because 'a' comes before 'b'. Now with Treets, the system cannot SORT them because it does not know how to compare two treets. That's why we need to tell it which criteria should be taken into account when comparing them. Apparently, Graig wants to compare the creation dates because he wants to sort them by this criterium. As for me, I would prefer to SORT them alphabetically by the first letter of Author (pretty much the same as the guys from Java development team did for Strings). But it is up to each and every one of us to decide on the criteria of comparison. That's why we need to override the method so that when we use sort(), it will SORT the objects by comparing them according to the criteria we want.

  • Question: ok, but why we use the compareTo() again with the dates, aren't we overriding it?? It comes from method overloading chapter. Recall, the methods are the same if their names and what they accept as the parameters type are the same. Should two methods have the same name but accept different parameters, these are two different methods (though with the same name). It is like taking two guys with the same name and say that they are basically one person - no way. Jimmy from California is not the same Jimmy from Texas. These are two different people. So, coming to the point, we are overriding the compareTo() in order to tell the system how to compare our Treets. And at one moment we say:
  • "You know what, the system, I want you to compare my treets by the Creation Date, so that when I call sort(), you will compare their Creation Dates and put my tweet in order from the most oldest to the newest ones. Got it?". And the system will say:
  • "Yeah, bros, no worries, I will do that." But then we ask:
  • "Wait, but do you know how to compare the dates??" And the system will say:
  • "Of course, those guys from Java told how to."

That's why when we write mCreationDate.compareTo(other.mCreationDate), the system recognizes the type of DATE and it knows how to compare it.

  • Question: Why then should we say that our Treet class implements the Comparable interface?? As Graig said, the interface is basically a contract that you sign. In this contract you will see that it obliges you to use its template. So, when other programmers deal with your class, they know that you have signed the contract, so basically your class includes the minimum requirements defined in this contract. The minimum requirement of Comparable interface (as much as I know), it is to use compareTo() method. You may override it or just leave it like it is, but you have to define it in you class by writing "public int compa...", you know the rest.

  • Question: what is "(equals(other))" I am less sure on but it basically compares two instances of your class. So, the first instance is the one you call compareTo() function from and another is the one that you passed inside the compareTo( here ) function. We can also write it in this way: this.equals(other), meaning that "this" relates to the instance of the class that you operate with. It might be a bit confusing but you have to imagine the process like the following:

You are in the gym and suddenly, you decide to see if your your biceps is as big as Schwartzenegger's ones. So, inside your head you call an equals() function. When you approach your biceps to the photo of Schwartzenegger's biceps, you compare it by inputing a parameter (size of the biceps) into your mental equals() method, just right in between the brackets. But you don't write myBicepts.equals(SchwartzeneggerBicepts), you just write: equals(SchwartzeneggerBicdepts), because you know that you compare it to "this." or your biceps.

Basically that's how I understand the video, sorry for the long answer, but I hope it explains what you wanted to clarify.