序:
在本文中,我们首先引入示例类,作为比较的基准:
public class Person{
private String name;
private int age;
public Person(){
}
public Person(String name, int age){
this.name = name;
this.age = age;
}
}
如上所示,定义了一个简单类:Person,其中有两个Private的成员变量name与age。(其中的所有Getter和Setter以及toString的覆写(Override)已省略)
如效仿其他的集合,使用TreeMap存储时(源码如下)......
public static void main(String[] args) {
TreeSet treeSet = new TreeSet(new SetDemo());
treeSet.add(new Person("xiaoliu",12));
treeSet.add(new Person("zhangsan",13));
treeSet.add(new Person("lisi",14));
treeSet.add(new Person("wangwu",15));
treeSet.add(new Person("zhaoxin",16));
treeSet.add(new Person("