/*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 }