Implementing the Comparable Interfa

Suppose we have an Employee class, with attributes employee number, name, and salary. We then create some Employee objects, and then add these to a SortedSet Collection.To keep these in a sorted order, we must implement the Comparable interface compareTo method. This method will define the sort order. The listing shows how we might do this for the Employee class.

Employee

class Employee implements Comparable
{
int empNumber;
String name;
int salary;

public Employee(int empNumber, String name, int salary)
{
this.empNumber = empNumber;
this.name = name;
this.salary = salary;
}

public boolean equals(Object o)
{
if (o == this)
{
return true;
}
if (o == null)
{
return false;
}
if (getClass() != o.getClass() )
{
return false;
}
Employee e = (Employee) o;
return empNumber == e.empNumber
&& name.equals(e.name)
&& salary == e.salary;
}

public int hashCode()
{
int result = 17;
result = 37 * result + empNumber;
result = 37 * result + name.hashCode();
result = 37 * result + salary
return result;
}

public int compareTo (Object o)
{

Employee e = (Employee) o;
return salary - e.salary;
}
}


In line 1, the Employee class definition indicates that the Comparable interface is being implemented. Lines 6–10 define the Employee constructor. In lines 12–26, we override the equals method, as discussed in Section 5.2.1. In this case, two employee objects are equal if the contents of their empNumber, name, and salary fields are all equal. In lines 28–35, we override the hashCode method, as discussed in Section 5.2.2. If we do not override hashCode, a collection of Employee objects will not behave correctly. In lines
37–40, we define the compareTo method.

The Java Language Specification states the properties that an overridden compareTo method should exhibit. The main requirement is that the expression x.compareTo(y) will return a negative integer, zero, or a positive integer if the object x is less than, equal to, or greater than the object y. In our case, we want to sort according to salary order; since
salary is a positive int, the expression salary - e.salary in line 39 satisfies the compareTo requirement.

CreateEmployee creates five employee objects, adds them to a TreeSet collection, and then prints the set.

CreateEmployee creates five employee objects, adds them to a TreeSet collection, and then prints the set.


CreateEmployee

import java.util.*;

class CreateEmployee
{
public static void main(String[] args)
{
Employee emp1 = new Employee(1,”jones”,15000);
Employee emp2 = new Employee(2,”sim”,20000);
Employee emp3 = new Employee(3,”jones”,19000);
Employee emp4 = new Employee(4,”thorpe”,18000);
Employee emp5 = new Employee(5,”jones”,19000);


Collection c = new TreeSet();
c.add(emp1);
c.add(emp2);
c.add(emp3);
c.add(emp4);
c.add(emp5);
Iterator i = c.iterator();
while (i.hasNext() )
{
Employee e = (Employee) i.next();

System.out.println(e.empNumber + '' " + e.name + " " + e.salary);
}
}
}



Note emp1 and emp3 are two different employees with the same surname JONES. Note that emp5 is a duplicate entry for employee number 3. The output, in salary order, is as follows:

> java CreateEmployee
1 JONES 15000
4 THORPE 18000
3 JONES 19000
2 SIM 20000


Note that, being a set, the collection does not include the duplicate emp5.

Suppose we want to sort by name and salary so that employees with the same name are ordered by salary. The code shows a new version of the Employee class compareTo method.

compareTo

public int compareTo(Object o)
{
Employee e = (Employee) o;
int cmp = name.compareTo(e.name);
return (cmp != 0 ? cmp: salary - e.salary) ;
}


In line 2, we cast the argument o to an Employee type. In line 3, we compare the most significant part of the objects, in this case, name. We use the String.compareTo method for this purpose: this uses a lexicographic ordering as we have seen. cmp will be set to zero if the objects are equal; otherwise, cmp will be nonzero. In line 4, if the value of cmp is nonzero, we return the value of cmp; otherwise, we compare the next significant part of the objects, namely, salary, and return the expression salary - e.salary.
If we now run CreateEmployee, the result will be as follows:

> java CreateEmployee
1 JONES 15000
3 JONES 19000
2 SIM 20000
4 THORPE 18000

No comments:

Post a Comment