Professional Documents
Culture Documents
Comparable Comparator
Comparable Comparator
• Comparable Interface
Comparator Interface
1
The Comparable Interface
2
The Comparable Interface Semantics
3
The Comparable Interface Semantics
4
The Comparable Interface
5
The Comparable Interface (cont’d)
6
The Comparable Interface (cont’d)
7
The Comparable Interface (cont’d)
8
Example
public class Student implements
Comparable<Student> {
@Override
public int compareTo(Student o) {
if (this.roll > o.roll)
return 1;
else if (this.roll < o.roll)
return -1;
else
return 0;
}
} 9
for (int i = 0; i < st.length; i++) {
st[i] = new Student();
st[i].setRoll(roll[i]);
public class StudentSimulator {
st[i].setName(name[i]);
public static void
list.add(st[i]);
main(String[] args) {
}
Student st[] =
System.out.println("\nStudent
new Student[3];
Information \n");
for (Student student : list) {
int roll[] = {
1003, 1001, 1002 }; System.out.println(student.getRoll(
String name[] = ) + "\t" + student.getName());
{ "javed", "sakina", "mahmud" }; }
Collections.sort(list);
ArrayList<Student> list = new
ArrayList<Student>();
10
The Comparator Interface
11
The Comparator Interface
12
The Comparator Interface
public interface Comparator
{
int compare(Object object1, Object object2);
boolean equals(Object object);
}
A class that implements Comparator should
implement the compare method such that its
returned value is:
0 if object1 “is equal to” object2
> 0 if object1 “is greater than” object2
< 0 if object1 “is less than” object2
13
The Comparator Interface (cont’d)
• It is also preferable for the compare
method to return 0 if and only if
object1.equals(object2) is true.
• The compare method throws a
ClassCastException if the type of object1
and that of object2 are not compatible for
comparison.
14
The Comparator Interface (cont’d)
• Example 2: This example sorts the strings in
reverse order of the alphabetical one.
import java.util.*;
class StringReverseComparator implements Comparator
{
public int compare(Object object1, Object object2)
{
String string1 = object1.toString();
String string2 = object2.toString();
// Reverse the comparison
return string2.compareTo(string1);
}
}
15
The Comparator Interface (cont’d)
class Test
{
public static void main(String[] args)
{
String[] array =
{"Ahmad","Mohammad","Ali","Hisham","Omar",
"Bilal","Hassan"};
Arrays.sort(array, new
StringReverseComparator());
System.out.println(Arrays.asList(array));
}
}
16
The Comparator Interface (cont’d)
-The sort method ,in the Arrays class,
sorts the array “array” according to the
comparator object. Notice the
comparator object is provided as a
parameter for the sorting method; it is
an object from the class
StringReverseComparator .
- After printing, we get the following
order:
[Omar, Mohammad, Hisham, Hassan,
Bilal, Ali, Ahmad]
17
Example class EmployeeSortBySal implements
Comparator<Employee> {
@Override
public int compare(Employee
o1, Employee o2) {
public class Employee return new
{ Float(o1.getSalary()).compareTo(o2.get-
Salary());
private int id; }
private String name; }
20