区别
Comparable 是在
集合内部
定义的方法实现的排序,只能有一种比较方式。在 lang 包下。
Comparator 是在集合外部
实现的排序,使用策略模式,可以自定义多种比较方式。在 util 包下。
Comparable
定义
package java.lang;
import java.util.*;
public interface Comparable<T> {
public int compareTo(T o);
}
用法
package com.zmz.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author 张明泽
* Create by 2022/9/2 15:12
*/
public class ComparableTest {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person(19,"xiaoming"));
list.add(new Person(26,"xiaomeng"));
list.add(new Person(18,"xiaozhang"));
Collections.sort(list);
System.out.println(list);
// [Person{age=18, name='xiaozhang'}, Person{age=19, name='xiaoming'}, Person{age=26, name='xiaomeng'}]
}
}
class Person implements Comparable{
int age;
String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public int compareTo(Object o) {
if (o instanceof Person) {
Person person = (Person) o;
return this.age - person.age;
}
return 0;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
Comparator
定义
用 Comparator 是 策略模式(strategy design pattern)
,就是不改变对象自身,而用一个策略对象(strategy object)来改变它的行为。
package java.util;
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
用法
package com.zmz.collection;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* @author 张明泽
* Create by 2022/9/2 15:33
*/
public class ComparatorTest {
public static void main(String[] args) {
List<Teacher> list = new ArrayList<>();
list.add(new Teacher(19,"xiaoming"));
list.add(new Teacher(26,"xiaomeng"));
list.add(new Teacher(18,"xiaozhang"));
list.sort(new AscTeacher());
System.out.println(list);
// [Teacher{age=18, name='xiaozhang'}, Teacher{age=19, name='xiaoming'}, Teacher{age=26, name='xiaomeng'}]
list.sort(new DescTeacher());
System.out.println(list);
// [Teacher{age=26, name='xiaomeng'}, Teacher{age=19, name='xiaoming'}, Teacher{age=18, name='xiaozhang'}]
// Collections.sort(list,new DescTeacher());
// 若 list 中为数字可以利用Lambda表达式简写
// Arrays.sort(arr, (a,b) -> a[0] - b[0]);
}
}
class Teacher {
int age;
String name;
public Teacher(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
class AscTeacher implements Comparator<Teacher> {
@Override
public int compare(Teacher o1, Teacher o2) {
return o1.age - o2.age;
}
}
class DescTeacher implements Comparator<Teacher> {
@Override
public int compare(Teacher o1, Teacher o2) {
return o2.age - o1.age;
}
}
10 条评论
《末日孤舰第二季》欧美剧高清在线免费观看:https://www.jgz518.com/xingkong/78860.html
真棒!
龙腾传世官网活动详情:https://501h.com/fugu/514.html
《保姆奇遇记》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/2931.html
你的文章让我心情愉悦,真是太棒了! https://www.4006400989.com/qyvideo/163.html
文章的确不错啊https://www.cscnn.com/
兄弟写的非常好 https://www.cscnn.com/
不错不错,我喜欢看 https://www.ea55.com/
不错不错,我喜欢看 https://www.237fa.com/
博主真是太厉害了!!!