Comparator: Comparator
is an interface which is defined in java.util package which means Comparator
should be used as a utility to sort objects. Comparator interface in Java has
method public int compare (Object o1, Object o2) which is used to compare two
different objects.
Let’s see below example.
Student.java
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.List;
class
Student {
private int marks;
private
String name;
/**
* @return the marks
*/
public int getMarks() {
return marks;
}
/**
* @return the name
*/
public
String getName() {
return name;
}
public
Student(int marks, String name){
this.marks = marks;
this.name = name;
}
/**
* @param args
*/
public static void
main(String[] args) {
List<Student>
studentList = new ArrayList<Student>();
studentList.add(new Student(50, "A"));
studentList.add(new Student(40, "B"));
studentList.add(new Student(60, "C"));
studentList.add(new Student(20, "D"));
studentList.add(new Student(30, "E"));
StudentSortByMarks
studentSortByMarks = new StudentSortByMarks();
Collections.sort(studentList,studentSortByMarks
);
for(Student st :
studentList) {
System.out.println(st.getMarks()
+ " :::: " + st.getName());
}
}
}
Let’s create an interface
StudentSortByMarks
StudentSortByMarks.java
import
java.util.Comparator;
public class
StudentSortByMarks implements Comparator<Student> {
public int compare(Student st1, Student st2) {
return st1.getMarks() - st2.getMarks();
}
}
Output:
Output:
20 :::: D
30 :::: E
40 :::: B
50 :::: A
60 :::: C
In the above example we have create a class Student.
Two instance variables marks and name are declared and values are assigned
through a constructor. Two getter methods are declared with which Student marks
and name are retrieved later.
The
Comparator is implemented by StudentSortByMarks where we are comparing marks of
students with method int
compare (Student st1, Student st2)
After that when we are running the main () method and After
the studentList is sorted, the elements are printed with enhanced for loop and above
output is getting generated.
Comparable: Comparable interface
in Java is defined in java.lang package. Comparable is implemented by a class
in order to be able to comparing object of itself with some other objects.
Comparable interface has method public int compareTo (Object o)
Let’s see below
example.
Student.java: The Student class implements Comparable
interface. Here, Comparable is designed to be generics;
that is, Comparable object compares only Student objects.
Two instance variables marks and name are
declared and values are assigned through a constructor. Two getter methods are
declared with which Student marks and name are retrieved later.
class Student implements
Comparable<Student> {
private int marks;
private String name;
/**
* @return the marks
*/
public int getMarks() {
return marks;
}
/**
* @return the name
*/
public String getName() {
return name;
}
public Student(int marks, String name)
{
this.marks = marks;
this.name = name;
}
public int compareTo(Student
st1) {
return this.marks - st1.marks;
}
}
StudentSortByMarks.java
Another class
StudentSortByMarks is created which creates some Student objects and adds them
to ArrayList studentList. The
ArrayList object studentList is passed to sort() method of Collections
class. The sort() method sorts as per the Comparable interface compareTo()
method as Student class implements Comparable interface.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StudentSortByMarks {
public static void main(String args[]) {
List<Student> studentList = new
ArrayList<Student>();
studentList.add(new Student(50, "A"));
studentList.add(new Student(40,
"B"));
studentList.add(new Student(60,
"C"));
studentList.add(new Student(20,
"D"));
studentList.add(new Student(30,
"E"));
Collections.sort(studentList);
for(Student st : studentList) {
System.out.println(st.getMarks() + " :
" + st.getName());
}
}
}
Output:
20 : D
30 : E
40 : B
50 : A
60 : C
Difference between Comparator and Comparable in Java
Parameter
|
Comparator
|
Comparable
|
Definition
|
Comparator
in Java is defined in java.util package which means Comparator should be used
as an utility to sort objects
java.util.Comparator
|
Comparable
interface is defined in java.lang package, which should be provided by
default
java.lang.Comparable
|
Sorting
logic
|
Sorting
logic is in separate class. Hence we can write different sorting based on
different attributes of objects to be sorted. E.g. Sorting using id, name
etc.
|
Sorting
logic must be in same class whose objects are being sorted. Hence this is
called natural ordering of objects
|
Sorting
Strategy
|
if
you want to sort on some other attribute of object then use Comparator
|
if
you want to sort objects based on natural order then use Comparable
|
Implementation
|
Class
whose objects to be sorted do not need to implement this interface. Some
other class can implement this interface.
Ex.
StudentSortByMarks class can
implement Comparator interface to sort collection of students object by marks
|
Class
whose objects to be sorted must implement this interface.
Ex. StudentSortByMarks
class needs to implement comparable to
collection of students object by marks
|
Sorting
method
|
int
compare(Object o1,Object o2)
This method compares o1 and o2 objects and returns a integer. Its value has following meaning. 1.positive – o1 is greater than o2 2. zero – o1 equals to o2 3. negative – o1 is less than o1 |
int
compareTo(Object o1)
This method compares this object with o1 object and returns a integer. Its value has following meaning
|
Calling
method
|
Collections.sort(List,
Comparator)
Here objects will be sorted on the basis of Compare method in Comparator |
Collections.sort(List)
Here objects will be sorted on the basis of CompareTo method |
No comments:
Post a Comment