This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding java.lang.Comparable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding java.lang.Comparable." Watch "Regarding java.lang.Comparable." New topic
Author

Regarding java.lang.Comparable.

Krishna Potluri
Greenhorn

Joined: Apr 20, 2007
Posts: 19
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

Joined: Mar 15, 2007
Posts: 108
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

Joined: Apr 20, 2007
Posts: 19
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

Joined: Feb 05, 2003
Posts: 234
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

Joined: Apr 20, 2007
Posts: 19
Hi Bart ,

Thanks for spending your valuable time to clear my doubt.

Thanks and Regards,
Kris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding java.lang.Comparable.
 
Similar Threads
issue with the optionsCollection,help
[Comparable] problem with ording classes
Please explain me this!
Fetch Exception In Entity Bean
Why The Output Is Same...