• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comaparable And comparator  RSS feed

 
Abhishek Bose
Greenhorn
Posts: 24
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I wrote this code but what is getting displayed is
[songs@addbf1, songs@42e816, songs@9304b1][songs@addbf1, songs@42e816, songs@9304b1]

Can somebody explain me why this is happening? Also in the comaprerTo method [return songName.compareTo(songName); falltopieces is comparing with falltopieces] its like comparing a song name with itself, Then what the use of compareTo()?


 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the "songs@addbf1" being printed, read about the default implementation of toString - the default implementation returns the name of the class@hashcode. So you would want to Override toString in your Songs class to may be print the song name.

Secondly, in the compareTo you must use s variable's songName to compare with the current instance songName
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And there is a difference between Comparator and Comparable. Both are not the same. though both help in ordering the elements of a collection.
 
Viktor Kubinec
Ranch Hand
Posts: 34
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you should follow java naming conventions ( class names should start with uppercase letter).

Second, your compareTo method implementation is not right (it is legal, but incorrect)

try this :



or this :




in compareTo method you should compare "this" object with s object. In your implementation you are comparing this object with itself, which makes no sense. To get a proper output you have to override toString method.
 
Abhishek Bose
Greenhorn
Posts: 24
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Viktor Kubinec wrote:First of all, you should follow java naming conventions ( class names should start with uppercase letter).

Second, your compareTo method implementation is not right (it is legal, but incorrect)

try this :



or this :




in compareTo method you should compare "this" object with s object. In your implementation you are comparing this object with itself, which makes no sense. To get a proper output you have to override toString method.




Thanks a lot... but i am still confused with how its working
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Bose wrote:
Thanks a lot... but i am still confused with how its working


What are you confused about. It's a method. It gets called. And, as the docs say, it returns negative, 0, or positive, to indicate that s.songName is less than, equal to, or greater than this.songName. (Which, by the way, is backwards for how one normally implements a comparison, but maybe somebody is doing a reverse sort.)

So what's your confusion?
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Bose wrote:
Thanks a lot... but i am still confused with how its working


String implements Comparable and overrides compareTo method. So you are comparing 2 songs based on their song name and since song names are in String you are making use of the String's compareTo implementation to check for similarity between 2 strings. So if they are similar that means you are dealing with same song.
 
Abhishek Bose
Greenhorn
Posts: 24
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Abhishek Bose wrote:
Thanks a lot... but i am still confused with how its working


What are you confused about. It's a method. It gets called. And, as the docs say, it returns negative, 0, or positive, to indicate that s.songName is less than, equal to, or greater than this.songName. (Which, by the way, is backwards for how one normally implements a comparison, but maybe somebody is doing a reverse sort.)

So what's your confusion?



According to my understanding in the first cycle this.songName gets value fastcar and s.songName gets the value falltopieces and then they get compared?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Bose wrote:
Jeff Verdegan wrote:
Abhishek Bose wrote:
Thanks a lot... but i am still confused with how its working


What are you confused about. It's a method. It gets called. And, as the docs say, it returns negative, 0, or positive, to indicate that s.songName is less than, equal to, or greater than this.songName. (Which, by the way, is backwards for how one normally implements a comparison, but maybe somebody is doing a reverse sort.)

So what's your confusion?



According to my understanding in the first cycle this.songName gets value fastcar and s.songName gets the value falltopieces and then they get compared?



I don't know what you mean by "first cycle," but the basic idea is this:

Sorting a list essentially consists of comparing various pairs of that list's elements to see if they're in the right order, and, if they're not, swapping them. (For a comparison sort. There are other kinds of sorting.)

So if we have a list: [1, 2, 3, 5, 4], and we sort it, at some point we might compare 1 and 2 and find out that they're in the right order, so we leave them, and at some point, we'll compare 5 and 4, find out that 5 > 4 and therefore should be after it in the list, and so we'll swap them.

There are many different sort algorithms, and what distinguishes one from the other is how they select which pairs of elements to compare. (You can google for bubble sort, insertion sort, merge sort, and others to see this.) One thing that they all have in common, though, is that they need a way to know if element X is less than, equal to, or greater than element Y.

So, when we call Collections.sort(list), it uses a particular algorithm--quick sort or merge sort, I think--to select pairs of elements for comparison to see if they're in the correct relative order or if they need to be swapped. It knows how to pick which pairs to compare, but it doesn't know how to tell where element X is less than element Y. Therefore, it either calls X.compareTo(Y) or comparator.compare(X, Y). Either way, the compare() or compareTo() method tells it which one is less, by returning negative, zero, or positive, according to rules appropriate for X and Y.

If X and Y are Integers, then the Integer.compareTo() method will check whether int x < int y. If X and Y are Strings, then String's copmareTo() method will iterate through the characters, comparing them until a difference is found, just like you would do if you were comparing two words for alphabetical ordering.

The sort() method doesn't have to know if we're comparing simple numerical ints, or Strings' characters, or Student objects' studentID values, or whatever. All it has to do is say, "Here: X.compareTo(Y), and give me negative, zero, or positve for X < Y, X = Y, or X > Y."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!