Comparator interface
Comparator interface is used to order the objects of
user-defined class.
This interface is found in
java.util package and contains 2 methods compare(Object obj1,Object obj2) and
equals(Object element).
It provides multiple sorting
sequence i.e. you can sort the elements based on any data member. For instance
it may be on rollno, name, age or anything else.
Syntax of compare method
public int compare(Object
obj1,Object obj2): compares
the first object with second object.
Collections class provides static methods for sorting
the elements of collection. If collection elements are of Set type, we can use
TreeSet. But We cannot sort the elements of List. Collections class provides
methods for sorting the elements of List type elements.
Method of Collections class
for sorting List elements
public void sort(List
list,Comparator c): is used
to sort the elements of List by the given comparator.
Example of sorting the elements of List that
contains user-defined class objects on the basis of age and name
In this example, we have
created 4 java classes:
- Student.java
- AgeComparator.java
- NameComparator.java
- Simple.java
Student.java
This class contains three
fields rollno, name and age and a parameterized constructor.
1. class Student{
2. int rollno;
3. String name;
4. int age;
5. Student(int rollno,String name,int age){
6. this.rollno=rollno;
7. this.name=name;
8. this.age=age;
9. }
10. }
AgeComparator.java
This class defines comparison
logic based on the age. If age of first object is greater than the second, we
are returning positive value, it can be any one such as 1, 2 , 10 etc. If age
of first object is less than the second object, we are returning negative
value, it can be any negative value and if age of both objects are equal, we
are returning 0.
1. import java.util.*;
2. class AgeComparator implements Comparator{
3. public int Compare(Object o1,Object o2){
4. Student s1=(Student)o1;
5. Student s2=(Student)o2;
6.
7. if(s1.age==s2.age)
8. return 0;
9. else if(s1.age>s2.age)
10. return 1;
11. else
12. return -1;
13. }
14. }
NameComparator.java
This class provides comparison
logic based on the name. In such case, we are using the compareTo() method of
String class, which internally provides the comparison logic.
1. import java.util.*;
2. class NameComparator implements Comparator{
3. public int Compare(Object o1,Object o2){
4. Student s1=(Student)o1;
5. Student s2=(Student)o2;
6.
7. return s1.name.compareTo(s2.name);
8. }
9. }
Simple.java
In this class, we are printing
the objects values by sorting on the basis of name and age.
1. import java.util.*;
2. import java.io.*;
3.
4. class Simple{
5. public static void main(String args[]){
6.
7. ArrayList al=new ArrayList();
8. al.add(new Student(101,"Vijay",23));
9. al.add(new Student(106,"Ajay",27));
10. al.add(new Student(105,"Jai",21));
11.
12. System.out.println("Sorting by Name...");
13.
14. Collections.sort(al,new NameComparator());
15. Iterator itr=al.iterator();
16. while(itr.hasNext()){
17. Student st=(Student)itr.next();
18. System.out.println(st.rollno+" "+st.name+" "+st.age);
19. }
20.
21. System.out.println("sorting by age...");
22.
23. Collections.sort(al,new AgeComparator());
24. Iterator itr2=al.iterator();
25. while(itr2.hasNext()){
26. Student st=(Student)itr2.next();
27. System.out.println(st.rollno+" "+st.name+" "+st.age);
28. }
29.
30.
31. }
32. }
Output:Sorting by Name...
106 Ajay 27
105 Jai 21
101 Vijay 23
Sorting by age...
105 Jai 21
101 Vijay 23
106 Ajay 27
No comments:
Post a Comment