[Java development path] (21) Comparator and Comparable
1. Comparable
package java.lang;
import java.util.*;
-
-
public interface Comparable {
public int compareTo(T o);
}
Note:
Comparable is the sorting interface. If a class implements the Comparable interface, the class can support sorting. If there is a List (OR array) of instances that implement the Comparable interface, the List (OR array) can be sorted by Collections. sort (or Arrays. sort.
Suppose we use x. compareTo (y) to "compare the size of x and y ". If "negative" is returned, it means "x is smaller than y". If "zero" is returned, it means "x is equal to y". If "positive" is returned, it means "x is greater than y ".
Example:
package com.qunar.test;
-
/**
* Created by xiaosi on 16-3-7.
*/
public class Student implements Comparable {
-
private String name;
private int age;
-
public Student(String name, int age) {
this.name = name;
this.age = age;
}
-
public String getName() {
return name;
}
-
public void setName(String name) {
this.name = name;
}
-
public int getAge() {
return age;
}
-
public void setAge(int age) {
this.age = age;
}
-
@Override
public int compareTo(Student stu) {
if(age == stu.getAge()){
return name.compareTo(stu.getName());
}//if
else if(age > stu.getAge()){
return 1;
}
return -1;
}
}
List stus = new ArrayList ();
stus.add(new Student("xiaosi",24));
stus.add(new Student("sunny",24));
stus.add(new Student("yoona",21));
stus.add(new Student("kim",27));
-
Collections.sort(stus);
for(Student stu : stus){
System.out.println("age" + stu.getAge() + " name->" + stu.getName());
}
The above example provides the following functions: sort by student age, and sort by name if the age is the same.
2. Comparator
package java.util;
-
-
public interface Comparator {
int compare(T o1, T o2);
boolean equals(Object obj);
}
Note:
If a class does not support sorting, and the Comparable interface is not implemented. Then we can establish a comparator of this class for sorting. This Comparator only needs to implement the Comparator interface. We can use the comparator and then sort the classes through the comparator.
Example:
package com.qunar.test;
-
/**
* Created by xiaosi on 16-3-7.
*/
public class Teacher {
private String name;
private int age;
-
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
-
public String getName() {
return name;
}
-
public void setName(String name) {
this.name = name;
}
-
public int getAge() {
return age;
}
-
public void setAge(int age) {
this.age = age;
}
}
Comparator:
package com.qunar.test;
-
import java.util.Comparator;
-
/**
* Created by xiaosi on 16-3-7.
* Teacher Comparator
*/
public class TeacherComparator implements Comparator {
-
@Override
public int compare(Teacher o1, Teacher o2) {
if(o1.getAge() == o2.getAge()){
return o1.getName().compareTo(o2.getName());
}//if
else if(o1.getAge() > o2.getAge()){
return 1;
}
return -1;
}
}
List teachers = new ArrayList ();
teachers.add(new Teacher("xiaosi",24));
teachers.add(new Teacher("sunny",24));
teachers.add(new Teacher("yoona",21));
teachers.add(new Teacher("kim",27));
Collections.sort(teachers,new TeacherComparator());
for(Teacher te : teachers){
System.out.println("age" + te.getAge() + " name->" + te.getName());
}
3. Comparison
Comparable is an interface that supports self-comparison (such as String and Integer ). Comparator is a dedicated Comparator. When this object does not support auto-comparison or the auto-comparison function cannot meet your requirements, you can write a Comparator to compare the sizes of two objects. One is to complete the comparison by yourself, and the other is to compare the implementation of external programs.
Using Comparator is a strategy design pattern. Instead of changing the object itself, a strategy object is used to change its behavior. For example, if you want to sort Integers by absolute values, Integer does not meet the requirements. You do not need to modify the Integer class (you cannot actually do this) to change its sorting behavior, you only need to use an object that implements the Comparator interface to control its sorting.
Comparable usage: Write the comparison method to the object class and declare all comparison rules externally. In this way, the Code has poor universality. If a comparison rule is changed, the Code must be rewritten.
Use of Comparator: in this way, the entity and comparison rules are unbound. The comparison rules can be called based on your own business needs.