• Post Reply Bookmark Topic Watch Topic
  • New Topic

stuck in comparable interface in java for sorting objects  RSS feed

 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im stuck at comparable intefaces..i know it was used to sorting objectss..but i need some clarity.in below program im confused for that overide compareto method.actually what here happening.
@Override
public int compareTo(Employee employee)
{
return this.salary - employee.salary;
}

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what here happening.

Look at the API doc for the Comparable class's compareTo method. It returns an int value that can be used to order the objects in the list that is being sorted.
It determines if this object is less than, equal to, or greater than the specified object.
The code uses it to order the objects based on the value of the salary variable. If this salary is 500 and the employee's salary is 600, the method would return -100.
What does the API doc say about negative values?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:Im stuck at comparable intefaces..i know it was used to sorting objectss..but i need some clarity.in below program im confused for that overide compareto method.actually what here happening.
@Override
public int compareTo(Employee employee)
{
return this.salary - employee.salary;
}


Take a look at the Javadoc ... https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Specifically ...
Java Documentation wrote: Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.


This, of course, is pretty straightforward.

As for how the Employee class does it, well, it does a little mathematical acrobatics. Just review the code a bit, with some cases, and you will see that the formula does result in what is wanted. If you can't figure it out, then IMHO, don't worry about it. Just modify the code that does the comparison, to return a negative, zero, or positive value as required.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you can learn from the basic idea, you should never write a compareTo() implementation like that. It doesn't work for some cases, such as when there is such a big difference between the salaries that an integer underflow occurs.

I would prefer to implement it like this:
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
what here happening.

Look at the API doc for the Comparable class's compareTo method. It returns an int value that can be used to order the objects in the list that is being sorted.
It determines if this object is less than, equal to, or greater than the specified object.
The code uses it to order the objects based on the value of the salary variable. If this salary is 500 and the employee's salary is 600, the method would return -100.
What does the API doc say about negative values?

negative value indicateds salary is less than employee salary am i right?
so here my doubt is
public int compareTo(Employee employee)/ here we are initializing the employee objects rite?
{
return this.salary - employee.salary; so here what is indicates actually which mean here substraction is happening ?so this.salary refers 75000 how?
}
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so this.salary refers 75000 how?

The compareTo method is located in an instance of the Employee class. this.salary refers to the variable in the same instance of the class. The Employee reference passed to the compareTo() method is the other object that is being compared to this object.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:
negative value indicateds salary is less than employee salary am i right?
so here my doubt is


What initialization of Employee object? This is just an method declaration.

Henry
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
so this.salary refers 75000 how?

The compareTo method is located in an instance of the Employee class. this.salary refers to the variable in the same instance of the class. The Employee reference passed to the compareTo() method is the other object that is being compared to this object.

so this.salary refers to be 80000 this one only na.i mean how it can initialize 2nd instance.

Henry Wong wrote:
sekhar kiran wrote:
negative value indicateds salary is less than employee salary am i right?
so here my doubt is


What initialization of Employee object? This is just an method declaration.

Henry

i mean we are passing those variables into that method rite
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Arrays class's sort() method uses the compareTo() method with two elements in the list of Employees to determine the sort order. The code probably uses an expression like this to determine the order:
oneEmployeeElement.compareTo(anotherEmployeeElement)
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:The Arrays class's sort() method uses the compareTo() method with two elements in the list of Employees to determine the sort order. The code probably uses an expression like this to determine the order:
oneEmployeeElement.compareTo(anotherEmployeeElement)

I mean I got your point but why should take like this 75000- 80000 ,I mean this.salary refers 2nd instance?

2) ineed exact function what Is happening in that comparetomethod which shows in example

waiting for reply.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have run your and mine program in debug mode. I found strange way of comparing these values with each other. In your program values that are compared is salary so here we have your insertion order of salary 80000, 750000, 120000 and 60000. While debuging in variable window after Arrays.sort(e); I saw values being compared in following fashion in public int compareTo(Employee employee)
this.salaryoperatoremployee.salary
75000-80000
120000-75000
120000-80000
60000-80000
60000-75000

Even in my program I added values in List. Insertion order 10, 6 and 60.While debuging in variable window after Collections.sort(myClassList); I saw value being compared in following fashion in public int compareTo(ComparableWithinClassDemo o).
this.classIdoperatoro.classId
6-10
60-6
60-10

I checked source code by clicking on Collections.sort() method it does cloning of this array of objects and does merge sort. It is quite obscure to understand. I don't think we need to know that much in detail. If anyone know will be great If explains
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:I have run your and mine program in debug mode. I found strange way of comparing these values with each other. In your program values that are compared is salary so here we have your insertion order of salary 80000, 750000, 120000 and 60000. While debuging in variable window after Arrays.sort(e); I saw values being compared in following fashion in public int compareTo(Employee employee)
this.salaryoperatoremployee.salary
75000-80000
120000-75000
120000-80000
60000-80000
60000-75000

Even in my program I added values in List. Insertion order 10, 6 and 60.While debuging in variable window after Collections.sort(myClassList); I saw value being compared in following fashion in public int compareTo(ComparableWithinClassDemo o).
this.classIdoperatoro.classId
6-10
60-6
60-10

I checked source code by clicking on Collections.sort() method it does cloning of this array of objects and does merge sort. It is quite obscure to understand. I don't think we need to know that much in detail. If anyone know will be great If explains

even i debugged i could see here my doubt is how this.salary refering 2nd instance and how employe.sal refers 1st. and how it was comparing and sorting values
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your problem solved now? If not, please explain.
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:. . . It doesn't work for some cases, such as when there is such a big difference between the salaries that an integer underflow occurs. . . .
You mean overflow, surely?

You cannot get an overflow from salaries because they are always positive (I think people would complain if they had a negative salary). Unless there are problems maintaining the class' invariants and the salary field can go negative, but that would be a problem that should be corrected inside the Employee class. There is a lot in the Java™ Tutorials about this very question.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Is your problem solved now? If not, please explain.

yeah i have a doubt ie how this.salary refering 2nd instance and how employe.sal refers 1st. and how it was comparing and sorting values
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a doubt ie how this.salary refering 2nd instance and how employe.sal refers 1st. and how it was comparing and sorting values

Are you asking how the sort() method works? The source for most of the java classes is included in the JDK folders.
Why are you concerned about how the sort method works?
It is not something to worry about. There are too many methods in the java classes to be able to understandd how they all work. If a method works, isn't that good enough?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:
yeah i have a doubt ie how this.salary refering 2nd instance and how employe.sal refers 1st. and how it was comparing and sorting values


The details of which elements compareTo() methods are used on which other elements, and the specifics of the sorting algorithm, are not officially documented. If you really need to know, then you have the source code. For the majority of us, we are probably fine with knowing that it is a modified merge sort, which in the future, may change.

Henry
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean when i debugg i could see
this.salary operator employee.salary
75000 - 80000
120000 - 75000
120000 - 80000
60000 - 80000
60000 - 75000

so how it takes the value for this.salary as well as employee.sal which was taken .
we inserted order of salary 80000, 750000, 120000 and 60000.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the source code for the sort method to see how it works.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before sorting of objects salary 80000 75000 120000 60000

so here how this.sal refers to 2nd instance of object salary dis is my first doubt
this.salary-emp.salary
75000-80000 = - value this.sal<emp.sal
so above condition shows negative value,after that is it going to sort or swap what wil happen?

after that again how the values taken ie this.sal and emp.sal for next step

120000-75000= + value this.sal>emp.sal

120000-80000 = + value this.sal>emp.sal

60000-80000 = - value this.sal<emp.sal

60000-75000= - value this.sal<emp.sal

finally how this sorting occurs?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:
finally how this sorting occurs?


As already mentioned a few times, the details of the sorting algorithm are not officially documented. You will need to examine the source code.

Henry
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finally how this sorting occurs?

The sorting is done in the sort method. Look at the source code to see how it works.

Or for your education, write a program that will do the sorting of the items in the list based on the value of the salary in each item in the list. When you have written the code, you will have the answer you are looking for.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
sekhar kiran wrote:
finally how this sorting occurs?


As already mentioned a few times, the details of the sorting algorithm are not officially documented. You will need to examine the source code.

Henry

k got you, so it will happens internally,so leave that one.how this.sal and emp.sal values taken from given object,
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Look at the source code for the sort method to see how it works.


how could i see, imean your taking about sorting in compartomethods?
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code must us an expression like the following to get an int value that can be used to order the Employee objects:
oneEmployee.compareTo(anotherEmployee)

 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:The code must us an expression like the following to get an int value that can be used to order the Employee objects:
oneEmployee.compareTo(anotherEmployee)


in that case it should be like 80000 comparto(75000) like that rite?
but in programing i see 75000-80000?this makes me confused/how this.sal works here this makes me annoying and confused
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:
in that case it should be like 80000 comparto(75000) like that rite?
but in programing i see 75000-80000?this makes me confused/how this.sal works here this makes me annoying and confused


What Norm described is how the sort algorithm calls the compareTo() method, and not what the compareTo() method is doing... So, to answer your question. It is right, and if you want to see it, you will need to look at the source for the sort(), and not the comparable method.

Henry
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I suggest that you write a small program with a loop that goes through the list of employees and compares each two elements in the list and prints a message that shows the two values of the salary and says which one is the smaller. Use the compareTo() method to determine which employee object has the smaller salary.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!