InterviewSolution
| 1. |
Comparable vs Comparator interface in Java |
|
Answer» There are two interfaces to sort the objects using the class data members. These are comparable interface and comparator interface. Details about these are given as FOLLOWS: Comparable InterfaceThe comparable interface provides a single SORTING sequence. This means that a single element of the collection can be used as the basis for the sorting. The compareTo() method is used by the comparable interface to sort the elements. The java.lang package contains the comparable interface. A program that demonstrates the comparable interface is given as follows: import java.io.*; import java.util.*; class Student implements Comparable<Student> { private int rollNumber;; private String name; private double MARKS; public int compareTo(Student s) { return this.rollNumber - s.rollNumber; } public Student(int rno, String n, double m) { this.rollNumber = rno; this.name = n; this.marks = m; } public int getRNo() { return rollNumber; } public String getName() { return name; } public double getMarks() { return marks; } } public class Demo { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<Student>(); list.add(new Student(23, "Harry", 87.5)); list.add(new Student(1, "AMY", 85.5)); list.add(new Student(15, "John", 55.0)); list.add(new Student(45, "James", 95.0)); list.add(new Student(7, "Sally", 78.0)); System.out.println("Original Student List: "); for (Student s: list) { System.out.println(s.getRNo() + " " + s.getName() + " " + s.getMarks()); } Collections.sort(list); System.out.println("\nStudent List after sorting according to their Roll Number: "); for (Student s: list) { System.out.println(s.getRNo() + " " + s.getName() + " " + s.getMarks()); } } }The output of the above program is as follows: Original Student List: 23 Harry 87.5 1 Amy 85.5 15 John 55.0 45 James 95.0 7 Sally 78.0 Student List after sorting according to their Roll Number: 1 Amy 85.5 7 Sally 78.0 15 John 55.0 23 Harry 87.5 45 James 95.0Comparator InterfaceThe comparator interface provides multiple sorting sequences. This means that multiple elements of the collection can be used as the basis for the sorting. The compare() method is used by the comparator interface to sort the elements. The java.util package contains the comparator interface. A program that demonstrates the comparator interface is given as follows: import java.io.*; import java.util.*; class Student implements Comparable<Student> { private int rollNumber;; private String name; private double marks; public int compareTo(Student s) { return this.rollNumber - s.rollNumber; } public Student(int rno, String n, double m) { this.rollNumber = rno; this.name = n; this.marks = m; } public int getRNo() { return rollNumber; } public String getName() { return name; } public double getMarks() { return marks; } } class MarksCompare implements Comparator<Student> { public int compare(Student s1, Student s2) { if (s1.getMarks() < s2.getMarks()) return -1; if (s1.getMarks() > s2.getMarks()) return 1; else return 0; } } class NameCompare implements Comparator<Student> { public int compare(Student s1, Student s2) { return s1.getName().compareTo(s2.getName()); } } public class Main { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<Student>(); list.add(new Student(23, "Harry", 87.5)); list.add(new Student(1, "Amy", 85.5)); list.add(new Student(15, "John", 55.0)); list.add(new Student(45, "James", 95.0)); list.add(new Student(7, "Sally", 78.0)); System.out.println("Original Student List: "); for (Student s: list) { System.out.println(s.getRNo() + " " + s.getName() + " " + s.getMarks()); } Collections.sort(list); System.out.println("\nStudent List sorted by Roll Number: "); for (Student s: list) { System.out.println(s.getRNo() + " " + s.getName() + " " + s.getMarks()); } System.out.println("\nStudent List sorted by Marks: "); MarksCompare mc = new MarksCompare(); Collections.sort(list, mc); for (Student s: list) { System.out.println(s.getRNo() + " " + s.getName() + " " + s.getMarks()); } System.out.println("\nStudent List sorted by Name: "); NameCompare nc = new NameCompare(); Collections.sort(list, nc); for (Student s: list) { System.out.println(s.getRNo() + " " + s.getName() + " " + s.getMarks()); } } }The output of the above program is as follows: Original Student List: 23 Harry 87.5 1 Amy 85.5 15 John 55.0 45 James 95.0 7 Sally 78.0 Student List sorted by Roll Number: 1 Amy 85.5 7 Sally 78.0 15 John 55.0 23 Harry 87.5 45 James 95.0 Student List sorted by Marks: 15 John 55.0 7 Sally 78.0 1 Amy 85.5 23 Harry 87.5 45 James 95.0 Student List sorted by Name: 1 Amy 85.5 23 Harry 87.5 45 James 95.0 15 John 55.0 7 Sally 78.0 |
|