We implemented the Comparable interface for custom sorts. When we performed a Comparator, we found a Comparator interface
Internet poor some information, summary notes.
The basic principle is comparison, and the underlying is a binary tree
Such as 3,6,5,1,7,4,9
When sorting, put 3 first, then 6 is bigger than 3, trouble such as 3 on the right, 5 is smaller than 6, put on the left, an analogy is the line plane this figure
First take a look at the Comparable interface definition
package java.lang;
import java.util.*;
public interface Comparable<T> {
public int compareTo(T o);
}Copy the code
Comparable sorts the objects of each class that implements it as a whole. The interface needs to be implemented by the class itself
Code Example Analysis
package com.list.customsort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestSort {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("fd", 20)); list.add(new Person("chy", 22)); list.add(new Person("wgj", 21)); System.out.println(list); Collections.sort(list); System.out.println(list); } } class Person implements Comparable<Person>{ private String name; private Integer age; public Person(String name, Integer age) { super(); this.name = name; this.age = age; } public StringgetName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Person o) {
// return this.getAge().compareTo(o.getAge());
returnthis.getName().compareTo(o.getName()); // Sort by name. abc } }Copy the code
The result is sorted by the name attribute of person:
Instead of using the collections.sort () method, you can operate directly with the treeSet collection
Set<Person> set = new TreeSet<>();
set.add(new Person("fd", 20)); set.add(new Person("chy", 22)); set.add(new Person("wgj", 21)); System.out.println(set);Copy the code
The result is exactly the same as above, which is sorted by name
Why, if you look at the Treeset source code, will you find the go method or the Compareto method
So using collections.sort (list) has the same effect as simply new TreeSet.
Comparator
Comparable is a Comparator interface. If we need to control the order of a class that does not support sorting, we can create a Comparator for that class. The Comparator only implements the Comparator interface. That is, we can implement a Comparator to create a new one, and then use the Comparator to sort the classes.
The interface definition
package java.util;
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}Copy the code
Code Example Analysis
package com.list.customsort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class TestSort2 {
public static void main(String[] args) {
List<Person2> list = new ArrayList<>();
list.add(new Person2("fd", 20)); list.add(new Person2("chy", 22)); list.add(new Person2("wgj", 21)); System.out.println(list); Collections.sort(list,new Comparator<Person2>() { @Override public int compare(Person2 o1, Person2 o2) {returno1.getAge().compareTo(o2.getAge()); }}); System.out.println(list); } } class Person2{ private String name; private Integer age; public StringgetName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person2(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person2 [name=" + name + ", age=" + age + "]"; }}Copy the code
The sort method of the main method can be simplified using lambda
Public static <T> void sort(List<T> List, Comparator<? Super T > c))
Collections.sort(list,(s1,s2)-> Integer.compare(s1.getAge(),s2.getAge()));Copy the code
The Treeset set is useless at this point.
Because it uses the compareTo method underneath.
Comparable and Comparator
Comparable is a sort interface, and if a class implements the Comparable interface, it means that “the class supports sorting.” The Comparator is a Comparator. If we need to control the order of a class, we can create a “Comparator of the class” for sorting.
Comparable is equivalent to “internal Comparator,” while Comparator is equivalent to “external Comparator.”
The two methods have their advantages and disadvantages. Comparable is simple, and objects that implement the Comparable interface are immediately Comparable, but require modifications to the source code. The advantage of using the Comparator is that you don’t need to modify the source code. Instead, you implement a Comparator. When a custom object needs to be compared, you pass the Comparator with the object so that it can be compared. Make it match something simpler, so you can save a lot of rework.
Ok, in place!!