/*TreeSet * treeSet存入数据后自动调用元素的compareTo(Object obj) 方法,自动对数据进行排序 * 所以输出的数据是经过排序的数据 * 注:compareTo方法返回值有:负数,零,正数。分别表示小于,等于,大于 * 对于存入自定义的对象元素,要重写元素的compareTo(Object obj)方法 * 元素定义时,需要实现Comparable接口 * */
1 import java.util.Iterator;
2 import java.util.TreeSet;
3 public class StudentCode {
4
5 public static void main(String []args){
6 //定义TreeSet对象,并赋值java存在的对象
7 TreeSet ts1=new TreeSet();
8 ts1.add("java10");
9 ts1.add("java01");
10 ts1.add("java08");
11 ts1.add("java04");
12 //输出对象的值,是经过排序的数据
13 System.out.println(ts1);
14 //定义TreeSet对象,并赋值自定义的对象
15 TreeSet ts2=new TreeSet();
16 ts2.add(new Person("ls",11));
17 ts2.add(new Person("zs",22));
18 ts2.add(new Person("ls",13));
19 ts2.add(new Person("ls",11));
20 //输出对象,也是经过排序的数据
21 for(Iterator it=ts2.iterator();it.hasNext();){
22 Person p=(Person)it.next();
23 System.out.println("姓名:"+p.getName()+",年龄:"+p.getAge());
24 }
25 }
26 }
27 //自定义数据,需要实现Comparable接口
28 class Person implements Comparable{
29 private String name;
30 private int age;
31 Person(String name,int age){
32 this.name=name;
33 this.age=age;
34 }
35 public String getName(){
36 return this.name;
37 }
38 public int getAge(){
39 return this.age;
40 }
41 //重写compareTo()方法,
42 public int compareTo(Object obj){
43 if(!(obj instanceof Person))
44 throw new RuntimeException("不是Person对象");
45 Person p =(Person)obj;
46 if(this.age>p.getAge()){
47 return 1;
48 }
49 else if(this.age<p.getAge()){
50 return -1;
51 }else{
52 return this.name.compareTo(p.getName());
53 }
54 }
55 }
1 /*TreeSet
2 * treeSet当元素不具备比较性,或者比较性不是所需要的时候,
3 * 可以使treeSet集合具有比较性。
4 * 定义比较器,并将比较器作为参数传给TreeSet集合
5 * 比较器需要实现Comparator接口
6 * 当元素具备比较性和比较器同时出现时,以比较器为准。
7 * */
8 import java.util.Comparator;
9 import java.util.Iterator;
10 import java.util.TreeSet;
11 public class StudentCode {
12
13 public static void main(String []args){
14 //定义TreeSet对象,并传入比较器
15 TreeSet ts2=new TreeSet(new MyCompareble());
16 ts2.add(new Person("ls",11));
17 ts2.add(new Person("zs",22));
18 ts2.add(new Person("ls",13));
19 ts2.add(new Person("ls",11));
20 //输出对象
21 for(Iterator it=ts2.iterator();it.hasNext();){
22 Person p=(Person)it.next();
23 System.out.println("姓名:"+p.getName()+",年龄:"+p.getAge());
24 }
25 }
26 }
27 //定义比较器 — — — — 以姓名为第一顺序,年龄为第二顺序
28 class MyCompareble implements Comparator{
29 //实现比较器里面的compare方法
30 public int compare(Object o1,Object o2){
31 Person p1=(Person)o1;
32 Person p2=(Person)o2;
33 int num=p1.getName().compareTo(p2.getName());
34 if(num==0){
35 return new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));
36 }
37 return num;
38 }
39 }
40
41 //自定义数据,需要实现Comparable接口 — — — — 以年龄为第一顺序,姓名为第二顺序
42 class Person implements Comparable{
43 private String name;
44 private int age;
45 Person(String name,int age){
46 this.name=name;
47 this.age=age;
48 }
49 public String getName(){
50 return this.name;
51 }
52 public int getAge(){
53 return this.age;
54 }
55 //重写compareTo()方法,
56 public int compareTo(Object obj){
57 if(!(obj instanceof Person))
58 throw new RuntimeException("不是Person对象");
59 Person p =(Person)obj;
60 if(this.age>p.getAge()){
61 return 1;
62 }
63 else if(this.age<p.getAge()){
64 return -1;
65 }else{
66 return this.name.compareTo(p.getName());
67 }
68 }
69 }