Github: https://github.com/yjfiejd/New_coder_java_algorithm/blob/master/comparator.java (可以下载code)
排序的算法是我们最常用的算法,初学程序,每个人都尝试过排序。但只是局限于简单的排序。
但是我们遇到的情况就不是如此简单了。如给公司里的商品进行排序,我们很轻易的想到按照商品的名称排序不就完了,而且简单明了。但现实并如我们相信般简单。同一商品名称可以有不同的批次,进货时间,可能还会有单价的不同。显然只根据商品名称排序是不合理的
如果对java比较熟悉的会知道java.util.Comparator 接口。要实现里面的函数
int compare(Object o1, Object o2) 返回一个基本类型的整型,返回负数表示o1 小于o2,返回0 表示o1和o2相等,返回正数表示o1大于o2
【转】Java中比较器小结
【转】java比较器Comparable接口和Comparator接口
【代码】
// 比较器的构造:
// 一个系统提供的有序的结构,都会伴随着比较器的构造,这个比较器告诉这个有序结构如何组织!! -> 可以用排序,优先级队列,红黑树
// 基于比较的排序
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import Code_09_Comparator.Student;
public class comparator {
// 主函数
public static void main(String[] args) {
Student student1 = new Student("A", 1, 23);
Student student2 = new Student("B", 2, 21);
Student student3 = new Student("C", 3, 22);
Student[] students = new Student[] {student3, student2, student1};
printStudent(students);
Arrays.sort(students, new IdAscendingComparator());
printStudent(students);
Arrays.sort(students, new IdDescendingComparator());
printStudent(students);
// 堆的方式:
// 系统提供的一个堆(优先级队列) // 如果不给比较器,它默认按内存地址排序,没有意思, 需要仍进去比较器
// 这里如果扔进去,id小的排在前面,则这是一个小根堆, -> 这里通过比较器构成了小根堆
System.out.println("*********************************************");
PriorityQueue<Student> heap = new PriorityQueue<>(new IdAscendingComparator());
heap.add(student3);
heap.add(student2);
heap.add(student1);
while (!heap.isEmpty()) {
Student student = heap.poll(); // 弹出堆
System.out.println("name: " + student.name +" "+ "id: " + student.id + " "+"Age: " + student.age );
}
}
// 1) 创建Student类, 内部包含3个变量,名字,id,年龄
public static class Student{
public String name;
public int id;
public int age;
public Student(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
}
// 2) 创建一个比较器类 -> id升序,继承
public static class IdAscendingComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// return o1.id - o2.id;
if(o1.id < o2.id) {
return -1; // 表示o1更小, 因为o1.id - o2.id < 0 , 为负数;第一个参数放前面。
}else if (o1.id > o2.id) {
return 1; // 表示o1更大, 因为o1.id - o2.id > 0 , 为正数;
}else {
return 0;
}
}
}
// 比较器 -> 年龄降序
public static class IdDescendingComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o2.age - o1.age; // 浓缩的写法,如果o2.age - o1.age > 0, 所有o2更大。
}
}
// 3) 打印函数如图
public static void printStudent(Student[] students) {
for (Student student:students) {
System.out.println("name: " + student.name +" "+ "id: " + student.id + " "+"Age: " + student.age );
}
System.out.println("============================================");
}
}
【结果】
name: C id: 3 Age: 22
name: B id: 2 Age: 21
name: A id: 1 Age: 23
============================================
name: A id: 1 Age: 23
name: B id: 2 Age: 21
name: C id: 3 Age: 22
============================================
name: A id: 1 Age: 23
name: C id: 3 Age: 22
name: B id: 2 Age: 21
============================================