Meaningless Drivel is fun!
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding java.lang.Comparable." Watch "Regarding java.lang.Comparable." New topic

Regarding java.lang.Comparable.

Krishna Potluri

Joined: Apr 20, 2007
Posts: 19

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:


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) { = id;
this.deptno = deptno; = 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;

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)));

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

Object element =;" "+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

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,
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

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

Thanks for spending your valuable time to clear my doubt.

Thanks and Regards,
wood burning stoves
subject: Regarding java.lang.Comparable.
It's not a secret anymore!