e.g. Consider that there are 4 students standing in a line in a random order.Now their instructor wants them to form a line according to their ascending heights.So the new line should have the first student with the least height, followed by a student who is taller than the first and so on.Now consider the instructor standing in front of the line and doing the re-arrangement.The assumption here is that the instructor is not blind and he knows how to compare heights of the students.So the instructor in his mind will do all the math and find the 1st smallest, then the 2nd, then 3rd and finally the 4th.So basically the instructor would be able to compare heights of all the boys one by one and compare.Thus each of the student should be comparable by height to other student.
Translating the above exaple in terms of java - Consider a simple class Student as follows -
// A non comparable class
public class Student {
private String firstName;
private String lastName;
private int height;
private int age;
// Public constructor
public Student(String fname, String lName, int h, int a) {
this.firstName = fName;
this.lastName = lName;
this.height = h;
this.age = a;
}
// Getter and Setter methods for all the
// instance fields if needed
}
Now consider we want to create multiple student objects and arrange them in a line with ascending heights.So as discussed earlier, all the student objects needs to be Comparable.Now how do you do that in Java? Comparable interface is the answer.Java provides the Comparable interface which has to be implemented by any class whose objects needs to be compared to one another for sorting purpose.This interface has the compareTo(Object o) which accepts the object that needs to be compared with the current object.Hence the implementing class must implement the compareTo() method.
So our new Student class would look as follows -
The new comparable class
public class Student implements Comparable {
private String firstName;
private String lastName;
private int height;
private int age;
Public constructor
public Student(String fName, String lName, int h, int a) {
this.firstName = fName;
this.lastName = lName;
this.height = h;
this.age = a;
}
Getter and Setter methods for all the
instance fields if needed
public String getFirstName() {
return this.firstName;
}
public int getHeight() {
return this.height;
}
This is the place which actually compares
students by height
It returns -1 if current students height -lt passed students height
It returns 1 if current students height -gt passed students height
It returns 0 if current students height = passed students height
public int compareTo(Object o) {
Student s = (Student) o;
if (this.height <>
return -1;
else if (this.height > s.height)
return 1;
else
return 0;
}
}
Now one can create multiple students and add them to TreeSet data structure so that they will be sorted according to the heights.
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class SortedStudents {
public static void main(String[] args) {
Student aditya = new Student("Aditya","Vaidya", 6, 25);
Student akhila = new Student("Akhila","Vaidya", 5, 24);
Student vivek = new Student("Vivek","Kumar", 4, 26);
// Sorry vivek ;)
Student roshan = new Student("Roshan","Dawrani", 7, 30);
Set students = new TreeSet();
students.add(aditya);
students.add(akhila);
students.add(vivek);
students.add(roshan);
Iterator sIterator = students.iterator();
while (sIterator.hasNext()) {
Student student = (Student) sIterator.next();
system.out.println("FNAME - " + student.getFirstName +
" HEIGHT - " + student.getHeight());
}
}
}
If you run this program the following would be its output -
FNAME - Vivek HEIGHT - 4
FNAME - Akhila HEIGHT - 5
FNAME - Aditya HEIGHT - 6
FNAME - Roshan HEIGHT - 7
You can see that irrespective of the order of insertion, the Students are sorted by height.
No comments:
Post a Comment