标签:tco 判断 列表 要求 map 存在 nal collect extend
public interface Comparable<T> { public int compareTo(T o); }
通过 x.compareTo(y) 来“比较对象x和y的大小”。若返回“负数”,意味着“x比y小”;返回“零”,意味着“x等于y”;返回“正数”,意味着“x大于y”。
public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); }
public class TestCompareComparatorAndComparable { private static class Student implements Comparable<Student> { private int age; private String grade; public Student(int age, String grade) { this.age = age; this.grade = grade; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return age + "-" + grade; } public boolean euuals(Student student) { if(this.age == student.age && this.grade == student.grade) { return true; } return false; } @Override public int compareTo(Student student) { return this.grade.compareTo(student.grade); } } //定义一个关于Student类的比较器 private static class StudentComparetorasc implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return (o1.getAge() - o2.getAge()); } } //定义一个降序比较器 private static class StudentComparetordesc implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return (o2.getAge() - o1.getAge()); } } public static void main(String[] args) { // 新建ArrayList(动态数组) ArrayList<Student> list = new ArrayList<>(); // 添加对象到ArrayList中 list.add(new Student(8,"grade2")); list.add(new Student(7,"grade1")); list.add(new Student(10,"grade4")); list.add(new Student(9,"grade3")); // 打印list的原始序列 System.out.printf("Original sort, list:%s\n", list); // 对list进行排序 // 这里会根据“student实现的Comparable<String>接口”进行排序,即会根据“grade”进行排序 Collections.sort(list); System.out.printf("Grade sort, list:%s\n", list); // 通过“比较器(AscAgeComparator)”,对list进行排序 // AscAgeComparator的排序方式是:根据“age”的升序排序 Collections.sort(list, new StudentComparetorasc()); System.out.printf("asc (age) sort, list:%s\n", list); // 通过“比较器(DescAgeComparator)”,对list进行排序 // DescAgeComparator的排序方式是:根据“age”的降序排序 Collections.sort(list, new StudentComparetordesc()); System.out.printf("Desc(age) sort, list:%s\n", list); // 判断两个person是否相等 testEquals(); } /** * @desc 测试两个Person比较是否相等。 * 由于Person实现了equals()函数:若两person的age、name都相等,则认为这两个person相等。 * 所以,这里的p1和p2相等 */ private static void testEquals() { Student p1 = new Student(10, "grade1"); Student p2 = new Student(11, "grade2"); if (p1.equals(p2)) { System.out.printf("%s EQUAL %s\n", p1, p2); } else { System.out.printf("%s NOT EQUAL %s\n", p1, p2); } } }
@SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> void sort(List<T> list) { list.sort(null); } @SuppressWarnings({"unchecked", "rawtypes"}) public static <T> void sort(List<T> list, Comparator<? super T> c) { list.sort(c); }
@SuppressWarnings({"unchecked", "rawtypes"}) default void sort(Comparator<? super E> c) { Object[] a = this.toArray(); Arrays.sort(a, (Comparator) c); ListIterator<E> i = this.listIterator(); for (Object e : a) { i.next(); i.set((E) e); } }
实际上Collections.sort底层就是i调用的Arrays.sort。
标签:tco 判断 列表 要求 map 存在 nal collect extend
原文地址:https://www.cnblogs.com/liujiarui/p/12603296.html