Comparator Interface
In my previous post Sorting of Objects using Comparable Interface With Very Easy Example ,I sorted Student type objects according to roll number.
Now scenario is you are writing a program in which you wants to create three sets of Student objects , one should sort according to roll number , one should sort according to name and one should sort according to percentage.
You can't use Comparable Interface here because by using comparable we can define only one order of sorting. To meet our specification we have to use comparator interface.
Comparator Interface have two methods compare() and equal(). We can pass Comparator reference as a parameter of Tree Set constructor.
Let's understand with example.
I have one Student POJO class.
Student.java
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 | package com.javasuitor.bean; import java.util.Comparator; public class Student{ private int rollNo; private String name; private double percentage; public Student() { // TODO Auto-generated constructor stub } public Student(int rollNo, String name, double percentage) { super(); this.rollNo = rollNo; this.name = name; this.percentage = percentage; } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPercentage() { return percentage; } public void setPercentage(double percentage) { this.percentage = percentage; } @Override public boolean equals(Object object) { // we need to type cast Object into Student type. Student student = (Student)object; if(this.rollNo == student.rollNo) return true; else return false; } @Override public int hashCode() { // hash code will be roll number of student. return this.rollNo; } public static Comparator<Student> rollNumberComparator = new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { // TODO Auto-generated method stub return s1.getRollNo()-s2.getRollNo(); } }; public static Comparator<Student> nameComparator = new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { // TODO Auto-generated method stub return s1.getName().compareTo(s2.getName()); } }; } |
I have created two comparator one will sort according to roll number and one will sort according to name.
Now check demo class
StudentTreeSetDemo.java
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 | package com.javasuitor.demo; import java.util.Iterator; import java.util.TreeSet; import com.javasuitor.bean.Student; public class StudentTreeSetDemo { public static void main(String[] args) { // This Tree Set store Student objects according to order of name TreeSet<Student> studentSet1 = new TreeSet<Student>( Student.nameComparator); // This Tree Set store Student objects according to order of roll number TreeSet<Student> studentSet2 = new TreeSet<Student>( Student.rollNumberComparator); // Student s1 = new Student(101, "Ashok", 82.2); Student s2 = new Student(102, "Jony", 80.3); Student s3 = new Student(103, "Devang", 76.0); Student s4 = new Student(104, "Sourabh", 74.2); Student s5 = new Student(105, "Aakash", 78.33); // Adding five objects into studentSet1 studentSet1.add(s1); studentSet1.add(s2); studentSet1.add(s3); studentSet1.add(s4); studentSet1.add(s5); // Adding same five objects into studentSet2 studentSet2.add(s1); studentSet2.add(s2); studentSet2.add(s3); studentSet2.add(s4); studentSet2.add(s5); // Print Students details System.out.println("Students in student Set 1 sorted according to name\n" ); Iterator<Student> studentIterator1 = studentSet1.iterator(); while (studentIterator1.hasNext()) { Student s = studentIterator1.next(); System.out.println("Roll No: " + s.getRollNo() + " Name: " + s.getName() + " Percentage: " + s.getPercentage()); } System.out.println("\n\nStudents in student Set 2 sorted according to roll number\n"); Iterator<Student> studentIterator2 = studentSet2.iterator(); while (studentIterator2.hasNext()) { Student s = studentIterator2.next(); System.out.println("Roll No: " + s.getRollNo() + " Name: " + s.getName() + " Percentage: " + s.getPercentage()); } } } |
We created two set studentSet1 and studentSet2 . They both have same data but in different order.
Let's check output of program
OUTPUT
Students in student Set 1 sorted according to name Roll No: 105 Name: Aakash Percentage: 78.33 Roll No: 101 Name: Ashok Percentage: 82.2 Roll No: 103 Name: Devang Percentage: 76.0 Roll No: 102 Name: Jony Percentage: 80.3 Roll No: 104 Name: Sourabh Percentage: 74.2 Students in student Set 2 sorted according to roll number Roll No: 101 Name: Ashok Percentage: 82.2 Roll No: 102 Name: Jony Percentage: 80.3 Roll No: 103 Name: Devang Percentage: 76.0 Roll No: 104 Name: Sourabh Percentage: 74.2 Roll No: 105 Name: Aakash Percentage: 78.33
I hope you understood the Comparator interface.
Please feel free to ask any question.
Happy learning.... :)
No comments:
Post a Comment