-
Notifications
You must be signed in to change notification settings - Fork 48
Comparable vs Comparator
Java provides two interfaces to sort objects using data members of the class: 1. Comparable 2. Comparator
But what is the difference?
Using Comparable Interface
Comparable interface supports a single sorting sequence which means that it compares elements based on a single elements. As well as that, using Comparable interface will affect your original object class since you need to override compareTo() method inside the class.
Using Comparator Interface
Unlike Comparable, Comparator interface supports multiple sorting sequences which means that it can compare elements based on multiple elements. As well as that, using Comparator interface will not affect your original class, since you need to create Comparator classes and override compare() method inside the comparator class.
For example, let's have a simple student class that has a name and GPA as attributes.
class Student{
String name;
double gpa;
public Student(String name, double cap){
this.name = name;
this.gpa = cap;
}
public String toString(){
return this.name + " GPA of " + this.gpa;
}
public String getName(){ return this.name; }
public double getGPA(){ return this.gpa; }
}
Using Comparable interface we need to implement Comparable interface and override the compareTo() method. Lets implement Comparable interface and override compareTo() method so that it can be sorted by descending order of GPA.
class Student implements Comparable<Student>{
String name;
double gpa;
public Student(String name, double cap){
this.name = name;
this.gpa = cap;
}
public String toString(){
return this.name + " GPA of " + this.gpa;
}
@Override
public int compareTo(Student other) {
if (this.getGPA() < other.getGPA()) return 1;
if (this.getGPA() > other.getGPA()) return -1;
else return 0;
}
public String getName(){ return this.name; }
public double getGPA(){ return this.gpa; }
}
Now we have done this, we can sort students by their GAP using Collection.sort(List students) Lets have driver class to test it out.
public class Compareman{
public static void main(String[] args){
ArrayList<Student> students = new ArrayList<Student>();
students.add(new Student("Brock", 2.1));
students.add(new Student("Josh", 1.1));
students.add(new Student("Bryan", 4.1));
students.add(new Student("James", 3.1));
Collections.sort(students);
System.out.println("Students sorted by GPA: ");
for(Student s : students){
System.out.println(s);
}
}
}
Doing this we will get following result
Students sorted by GPA:
Bryan GPA of 4.1
James GPA of 3.1
Brock GPA of 2.1
Josh GPA of 1.1
Now what if we want to compare students by multiple elements such as Name and GPA?
Now lets use Comparator interface to see how we can achieve that. Using Comparator interface, we need to create separate Comparator object class for each element that you want to compare and override the compare() method inside each class however we do not need to modify our original Student class.
class Student{
String name;
double gpa;
public Student(String name, double cap){
this.name = name;
this.gpa = cap;
}
public String toString(){
return this.name + " GPA of " + this.gpa;
}
public String getName(){ return this.name; }
public double getGPA(){ return this.gpa; }
}
class GpaComparator implements Comparator<Student> {
public int compare(Student s1, Student s2) {
if (s1.getGPA() < s2.getGPA()) return 1;
if (s1.getGPA() > s2.getGPA()) return -1;
else return 0;
}
}
class NameComparator implements Comparator<Student> {
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
}
Now we have Comparator object classes for each GAP and name, we can compare for both of them in out driving class using Collection.sort(List students, Comparator comparator)
public class Compareman{
public static void main(String[] args){
ArrayList<Student> students = new ArrayList<Student>();
students.add(new Student("Josh", 1.1));
students.add(new Student("Brock", 2.1));
students.add(new Student("James", 3.1));
students.add(new Student("Bryan", 4.1));
System.out.println("Students sorted by name: ");
NameComparator compareByName = new NameComparator();
Collections.sort(students, compareByName);
for(Student s : students){
System.out.println(s);
}
System.out.println("Students sorted by GPA: ");
GpaComparator compareByGpa = new GpaComparator();
Collections.sort(students, compareByGpa);
for(Student s : students){
System.out.println(s);
}
}
}
Doing this will get following result
Students sorted by name:
Brock GPA of 2.1
Bryan GPA of 4.1
James GPA of 3.1
Josh GPA of 1.1
Students sorted by GPA:
Bryan GPA of 4.1
James GPA of 3.1
Brock GPA of 2.1
Josh GPA of 1.1
Peer Learning
Codecrunch Contributions
Piazza Contributions
Wiki Contributions
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Sunfire
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
CS2030 Contents
Lecture 1 SummaryCompile-run vs Run-time Summary
Quick Guide To Abstraction
Generics and Variance of Types
Comparable vs Comparator
Summary of completable future
CS2030S Notes
ELI5 Optional.map vs Optional.flatMap
PECS Example Code
Java Collection Framework (Iterator)
Generic
Generic Type Parameter and Generic Wildcard
Calculator
Lambda-Expression
Single Abstract Method (SAM)
Method Reference
Functional Interfaces 2
Simple Usage of Sandbox
Associative-but-not-commutative
Higher Order function
Functional Programming
Calculator With Functor
Eager Evaluation VS Lazy Evaluation
Simple Usage of Lazy Evaluation
Lazy Evaluation for LazyList
Lazy Evaluation for BinaryTree
Stream
Parallel Stream
Optional
Simple Usage of Stream
Asynchronous Programming
Notes on CompletableFuture
Notes on CompletableFuture 2
Simple Usage of CompletableFuture
Mind Map
Exception Handling
Links
CS2030 Java Style Guide
CS2030 Javadoc Specification
JDK 11 Download Link
JDK 11 API Docs
Codecrunch
Piazza Forum