Spaces:
Running
Running
File size: 4,988 Bytes
79054f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
Interface Comparable
When an object implements the INTERFACE 'Comparable', it can be compared to OTHER OBJECTS (USU instances of the SAME CLASS).
The METHOD defined in the interface is called 'compareTo', and is utilized for example by the sorting methods in Java.
This means that if your class implements the interface,
=> LATER WHEN U CREATE a 'LIST containing OBJECTS created FROM THAT CLASS'
=> THE LIST OF OBJECTS can be ordered by using the 'sort' method from class 'Collections'.
Method 'compareTo' RETURNS an INTEGER by the following rules:
- the method returns a 0, if the values are equal
- the method returns a NEGATIVE value if the current (this) object is "SMALLER" than the other object
- the method returns a POSITIVE value if the current (this) object is "LARGER" than the other object
======================================================
Note that the programmer must decide on how the actual comparison is made.
For example, the order of two Student objects could be determined based on the
- student id,
- number of credits, or for example
- name (in alphabetical order).
Hence, it is important to think what is the correct method in the current situation.
The interface uses GENERICS.
This is discussed in detail in the last week of the course.
Here, it means that the class defines the types of objects used in the comparison.
In practice, it is almost always the same as the class itself: students are compared to other students, cars to other cars, and files to other files.
Let's consider a class 'Student' which implements the 'Comparable' interface.
The property used for comparison is the 'number of credits'.
// Generic type definition for implementing the interface
class Student implements Comparable<Student> {
private String studentId;
private String name;
private int credits;
// CONSTRUCTOR
public Student(String studentId, String name, int credits) {
this.studentId = studentId;
this.name = name;
this.credits = credits;
}
@Override
public String toString() {
return name + " (" + studentId + "), " + credits + " op.";
}
@Override
public int compareTo(Student o) {
if (this.credits == o.credits) {
return 0;
}
else if (this.credits < o.credits) {
return -1;
}
else {
return 1;
}
}
}
For clarity, above, there was an entire conditional statement written in the method 'compareTo',
but naturally, we could get the same result with a single expression:
return this.credits - o.credits
======================================================
Now, we can SORT a list of student objects into natural order based on their credits:
import java.util.ArrayList;
import java.util.Collections;
public class SortTest {
public static void main(String[] args) {
// CREATE OBJECTS
Student o1 = new Student("1234", "Oscar", 15);
Student o2 = new Student("4321", "Olive", 25);
Student o3 = new Student("3333", "Paul", 7);
Student o4 = new Student("9999", "Pauline", 19);
// CREATE A LIST OF OBJECTS
ArrayList<Student> students = new ArrayList<>();
students.add(o1);
students.add(o2);
students.add(o3);
students.add(o4);
// SORT HERE
Collections.sort(students);
for (Student o : students) {
System.out.println(o);
}
}
}
Program output:
Paul (3333), 7 op.
Oscar (1234), 15 op.
Pauline (9999), 19 op.
Olive (4321), 25 op.
====================================================================
As another example, let's consider the class Car.
In the example, the cars are sorted based on their 'make'.
Note that we can "use the String class implementation of the Comparable class's " compareTo in our own class.
class Car implements Comparable<Car> {
private String make;
private String model;
public Car(String make, String model) {
this.make = make;
this.model = model;
}
@Override
public String toString() {
return make + " " + model;
}
// DEFAULT 'compareTo' COMPARES STRINGS IN ALPHABETICAL ORDER
@Override
public int compareTo(Car o) {
return this.make.compareTo(o.make);
}
}
Example of sorting an array of car objects:
import java.util.Arrays;
public class Example {
public static void main(String[] args) {
// CREATE OBJECTS
Car a1 = new Car("Volvo", "440");
Car a2 = new Car("Porsche", "911");
Car a3 = new Car("Datsun", "100a");
Car a4 = new Car("Citroen", "C5");
// CREATE ARRAY OF OBJECTS
Car[] cars = {a1, a2, a3, a4};
// SORT ARRAY
Arrays.sort(cars);
for (Car car : cars) {
System.out.println(car);
}
}
}
Program output:
Citroen C5
Datsun 100a
Porsche 911
Volvo 440
|