Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding java.lang.Comparable.

 
Krishna Potluri
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

order of an elements in a java collection can be either in natural order of the data in the collection [i.e. if that collection implemnts java.lang.Comparable interface] or data can be in any order if that collection implements java.lang.Comparator.

My doubt is below Employee code implements java.lang. Comparable interface.

___________________________________________________________________________
I would like to print Employees based on the DEPTNO. I am getting the following OUTPUT:

com.kris.tigerv.Employee@ef22f8
com.kris.tigerv.Employee@52fe85
com.kris.tigerv.Employee@c40c80
com.kris.tigerv.Employee@110d81b
__________________________________________________________________________

Kindly clear my DOUBT?

Code Is:
___________________________________________________________________________
//Here Category is Log4J class.

package com.kris.tigerv;
import java.lang.Comparable;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.log4j.Category;

class Employee implements Comparable{
private int id =0;
private String name = null;
private String job = null;
private int deptno = 0;
private Date hiredate = null;

Employee(int id,int deptno,String name,String job,Date hiredate) {
this.id = id;
this.deptno = deptno;
this.name = name;
this.job = job;
this.hiredate = hiredate;
}

public int compareTo(Object o){
if (this.deptno == ((Employee)o).deptno){
return 0;
}
else if(this.deptno < ((Employee)o).deptno){
return -1;
}
else{

return 1;
}


}
}

public class Emp_Comparable {
public static void main(String[] args) {
List list = new ArrayList();

list.add(new Employee(1,4,"Sri Krishna","Consultant",new Date()));
list.add(new Employee(2,3,"Sri Ram","Analyst",new Date(1975,7,6)));
list.add(new Employee(3,2,"Lakshmi Prasad","Architect",new Date(1973,10,23)));
list.add(new Employee(4,1,"Gopal","Developer",new Date(1971,7,15)));


Collections.sort(list);
Category category = Category.getInstance(Emp_Comparable.class);
Iterator iterator = list.iterator();

while(iterator.hasNext()){
Object element = iterator.next();
category.info(" "+element);

}


}
}

_____________________________________________________

Thanks in advance,
Kris Potluri
 
swarna dasa
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't it be a good idea to override the toString method?

public String toString() {
System.out.println( deptno+", "+ name+", "+job+", "+hiredate
}
 
Krishna Potluri
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swarna,

When i override the toString() in the Employee class i am getting correct output.

Thanks for spending your most valuable time for clearing my doubt.

Thanks and Regards,
Krishna.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, sorting on departement number does not seems the 'natural' order of an Employee (but this is of course also business based).

I would probably write the compareTo based on the employee's name (or maybe some kind of employeeNumber).
Additionally I would write a Comparator based on the departement number.

When calling Collections.sort(listOfEmployees), they are sorted based on there natural order.
When calling Collections.sort(listOfEmployees, comparator), they are sorted based on the comparator.

(Another option is to directly create a sorted Collection (like TreeSet) which would insert each element directly in the correct place).
 
Krishna Potluri
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bart ,

Thanks for spending your valuable time to clear my doubt.

Thanks and Regards,
Kris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic