I. Introduction to the scene
The Comparable and Comparator interfaces are used to specify sorts for our data structures.
The Comparable interface defines a method:
public interface Comparable<T> {
public int compareTo(T o);
}
Copy the code
The comparator interface defines methods (some classes implement comparators, but don’t implement equals because the Object class already implements equals) :
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
Copy the code
The comparable interface is the main difference from the Comparator interface
- Comparable objects are directly comparable, but only if the methods are defined in the class.
- 2. The comparator does not modify the entity class.
Introduction to the usage of the two interfaces
public class Student implements Comparable<Student> {
private String name;
private int 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;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
@Override
public int compareTo(Student o) {
if(this.age == o.getAge() &&this.name == o.getName()){
return 0;
}else if(this.age > o.getAge()){
return 1;
}else{
return- 1; } } @Override public StringtoString() {
return "Student[name="+this.name+",age="+this.age+"]"; }}Copy the code
Main method test
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("Hello1", 32)); list.add(new Student("Hello2", 100)); list.add(new Student("Hello3", 19)); list.add(new Student("Hello4", 34)); System.out.println("= = = = = = = = = = = = = = = = = = = not sorted result = = = = = = = = = = = = = = = = = = = = = = = =");
for (Student student : list) {
System.out.println(student.toString());
}
System.out.println("= = = = = = = = = = = = = = = the ranking results Comparable interface = = = = = = = = = = = = = = = = =");
Collections.sort(list);
for (Student student : list) {
System.out.println(student.toString());
}
System.out.println("= = = = = = = = = = = = = = = the ranking results Comparator interface = = = = = = = = = = = = = = = = =");
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.age == o1.getAge() &&o1.name == o2.getName()){
return 0;
}else if(o1.age < o2.age){
return 1;
}else{
return- 1; }}});for (Student student : list) {
System.out.println(student.toString());
}
System.out.println("===============lambda expression sort result ===================");
Collections.sort(list,(o1,o2)->(o1.getAge()-o2.getAge()));
list.forEach(student->{
System.out.println(student.toString());
});
}
Copy the code
The execution result
= = = = = = = = = = = = = = = = = = = not sorted result = = = = = = = = = = = = = = = = = = = = = = = = Student [name = Hello1, age = 32] Student [name = Hello2, age = 100] Student [name = Hello3, age = 19] Student [name = Hello4, age 34] = = = = = = = = = = = = = = = = the ranking results Comparable interface = = = = = = = = = = = = = = = = = Student[name=Hello3,age=19] Student[name=Hello1,age=32] Student[name=Hello4,age=34] Student[name=Hello2,age=100] = = = = = = = = = = = = = = = the ranking results Comparator interface = = = = = = = = = = = = = = = = = Student [name = Hello2, age = 100] Student [name = Hello4, age = 34] Student [name = Hello1, age = 32] Student [name = Hello3, age 19] = = = = = = = = = = = = = = = = lambda expressions sorting result = = = = = = = = = = = = = = = = = = = Student[name=Hello3,age=19] Student[name=Hello1,age=32] Student[name=Hello4,age=34] Student[name=Hello2,age=100]Copy the code