This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging
introduce
Java provides two interfaces, java.lang.Comparable and java.util.Comparator, when you need to compare the size of two objects in a customized way
Natural order Comparable
Let the two classes that need to compare objects implement the Comparable interface.
1. For a class to use the sort service, it must implement the comparaeTo method. This is because the comparator is provided to the sort method.
2. Any class that implements the Comparable interface needs to include the compareTo method.
@Override
public int compareTo(Student o) {
return this.id - o.id;
}
Copy the code
Note that the subtraction technique here does not apply to floating point values
Comparator sort
A Comparator is an instance of a class that implements the Comparator interface. We need this instance to call the compare method to complete the comparison
@Override
public int compare(Student o1, Student o2) {
return o1.getId() - o2.getId();
}
Copy the code
Order from smallest to largest
//return o1.getId() – o2.getId();
Order from largest to smallest
//return o2.getId() – o1.getId();
The difference:
1. The advantage of using a comparator is that the comparison algorithm can be separated from the concrete class, reducing the coupling between classes.
There are times when the same object needs to be sorted in multiple ways, something that natural sorting cannot do with Comparable. For example, we sorted a set of student objects by ID, but in some cases we need to sort by age.
3. Sort an array of strings because the String class implements Comparable and the String.compareTo method compares strings in lexicographical order. So if we want to sort strings in ascending order of length, instead of sorting them lexicographically. We certainly can’t make the String class implement the compareTo method in two different ways, and we shouldn’t modify the String class. This is where the comparator comes in.
Comparable is a class that defines the nature of the comparison at the time it is created. Comparator is a tool class that is created to perform the comparison. Comparator is more flexible and does not change the class itself, reducing the coupling.
For example:
Select Student (id) from Student (id) to Student (id);
Method 1: Implement the Comparable interface
import java.util.*;
public class ComparableTest {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student student1 = new Student(1."student1");
Student student2 = new Student(2."student2");
Student student3 = new Student(3."student3");
list.add(student1);
list.add(student3);
list.add(student2);
// Order the student objects in the list by id from smallest to largest
// The Sort method in the Collections class can sort Collections that implement the List interface, assuming that the List elements implement the Comparable interfaceCollections.sort(list); System.out.println(list); }}Copy the code
class Student implements Comparable<Student> {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString(a) {
return "Student{" +
"id=" + id +
", name='" + name + '\' ' +
'} ';
}
@Override
public int compareTo(Student o) {
return this.id - o.id; }}Copy the code
Method 2: Use a Comparator (a Comparator is an instance of a class that implements the Comparator interface)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ComparatorTest {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student student1 = new Student(1."student1");
Student student2 = new Student(2."student2");
Student student3 = new Student(3."student3");
list.add(student1);
list.add(student3);
list.add(student2);
// Order the student objects in the list by id from smallest to largest
list.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
returno1.getId() - o2.getId(); }}); System.out.println(list); }}class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId(a) {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(a) {
return "Student{" +
"id=" + id +
", name='" + name + '\' ' +
'} '; }}Copy the code