我们都知道,要对自建对象按照一定规则进行排序的话,要求自建对象实现Comparable接口,并重写compareTo() 方法,但compareTo() 方法的释义却不是那么容易搞清楚,下面举例进行阐述: 声明一个Student类(成员变量id,name)实现Comparable接口,然后重写compareTo()方法, public class Student implements Comparable<Student>{ private int id; private String nam…
public class TreeMapTest { public static void main(String[] args) { Map<Student,Integer> students = new TreeMap<>(); students.put(new Student("11"),1); students.put(new Student("11"),1); System.out.println(students.size());…
package cn.learn.collection.Collections; /* 排序的对象的类,实现comparable借口,重写compareto方法 若要打印必须重写toString方法,会默认调用 */ public class Person implements Comparable<Person>{ private String name; private int age; @Override public String toString() { return "P…