1. 程式人生 > >TreeSet、Comparable、Comparator、內部類、匿名類

TreeSet、Comparable、Comparator、內部類、匿名類

複製程式碼
 1 package com.tn.treeSet;
 2 
 3 public class Student {
 4     private String name;
 5     private int age;
 6     public Student(){}
 7     public Student(String name, int age) {
 8         super();
 9         this.name = name;
10         this.age = age;
11     }
12     public String getName() {
13 return name; 14 } 15 public void setName(String name) { 16 this.name = name; 17 } 18 public int getAge() { 19 return age; 20 } 21 public void setAge(int age) { 22 this.age = age; 23 } 24 @Override 25 public String toString() { 26 return
"Student [name=" + name + "]"; 27 } 28 }
複製程式碼 複製程式碼
 1 package com.tn.treeSet;
 2 
 3 import java.util.TreeSet;
 4 
 5 public class TreeSetDemo {
 6     public static void main(String[] args){
 7         TreeSet<Student> students=new TreeSet<>();//後一個尖括號內可以省略型別
 8         Student student1=new
Student("武松",30); 9 Student student2=new Student("林沖",31); 10 Student student3=new Student("魯智深",29); 11 students.add(student1); 12 students.add(student2); 13 students.add(student3); 14 System.out.println(students); 15 } 16 }
複製程式碼

 執行結果:

原因:TreeSet中加入的物件需要能進行比較,即實現Comparable介面

 改造一:在Student類中實現Comparable介面

複製程式碼
 1 package com.tn.treeSet;
 2 
 3 public class Student implements Comparable<Student> {
 4     private String name;
 5     private int age;
 6     public Student(){}
 7     public Student(String name, int age) {
 8         super();
 9         this.name = name;
10         this.age = age;
11     }
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     public int getAge() {
19         return age;
20     }
21     public void setAge(int age) {
22         this.age = age;
23     }
24     @Override
25     public String toString() {
26         return "Student [name=" + name + "]";
27     }
28     @Override
29     public int compareTo(Student o) {
30         if(!this.equals(o)){
31 //            return this.name.compareTo(o.name);
32             return o.name.compareTo(this.name);//和上面語句列印順序顛倒。
33         }
34         return 0;
35     }    
36 }
複製程式碼 複製程式碼
 1 package com.tn.treeSet;
 2 
 3 import java.util.TreeSet;
 4 
 5 public class TreeSetDemo {
 6     public static void main(String[] args){
 7         TreeSet<Student> students=new TreeSet<>();//後一個尖括號內可以省略型別
 8         Student student1=new Student("武松",30);
 9         Student student2=new Student("林沖",31);
10         Student student3=new Student("魯智深",29);
11         students.add(student1);
12         students.add(student2);
13         students.add(student3);
14         System.out.println(students);
15     };//方法體結結尾大括號後有;不會報錯
16 };//類體最後一個大括號後有;不會報錯
複製程式碼

改造二:用內部類實現Comparator介面

複製程式碼
 1 package com.tn.treeSet;
 2 
 3 public class Student{
 4     private String name;
 5     private int age;
 6     public Student(){}
 7     public Student(String name, int age) {
 8         super();
 9         this.name = name;
10         this.age = age;
11     }
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     public int getAge() {
19         return age;
20     }
21     public void setAge(int age) {
22         this.age = age;
23     }
24     @Override
25     public String toString() {
26         return "Student [name=" + name + "]";
27     }
28 }
複製程式碼 複製程式碼
 1 package com.tn.treeSet;
 2 
 3 import java.util.Comparator;
 4 import java.util.TreeSet;
 5 
 6 public class TreeSetDemo {
 7     public static void main(String[] args){
 8         Student student1=new Student("武松",30);
 9         Student student2=new Student("林沖",31);
10         Student student3=new Student("魯智深",29);
11         //TreeSet構造時用Comparator作為建構函式引數
12         TreeSet<Student> students=new TreeSet<Student>(new ComparatorDemo());
13         students.add(student1);
14         students.add(student2);
15         students.add(student3);
16         System.out.println(students);
17     }
18     static class ComparatorDemo implements Comparator<Student>{
19 //        內部類要寫在類體裡,但不能寫進類中方法體內。
20         @Override
21         public int compare(Student o1, Student o2) {
22             return o1.getName().compareTo(o2.getName());
23         }        
24     }
25 }
複製程式碼

 改造三:用匿名類實現Comparator介面

複製程式碼
 1 package com.tn.treeSet;
 2 
 3 public class Student{
 4     private String name;
 5     private int age;
 6     public Student(){}
 7     public Student(String name, int age) {
 8         super();
 9         this.name = name;
10         this.age = age;
11     }
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     public int getAge() {
19         return age;
20     }
21     public void setAge(int age) {
22         this.age = age;
23     }
24     @Override
25     public String toString() {
26         return "Student [name=" + name + "]";
27     }
28 }
複製程式碼 複製程式碼
 1 package com.tn.treeSet;
 2 
 3 import java.util.Comparator;
 4 import java.util.TreeSet;
 5 
 6 public class TreeSetDemo {
 7     public static void main(String[] args) {
 8         Student student1 = new Student("武松", 30);
 9         Student student2 = new Student("林沖", 31);
10         Student student3 = new Student("魯智深", 29);
11         
12         TreeSet<Student> students = new TreeSet<Student>(
13                 new Comparator<Student>() {
14                     @Override
15                     public int compare(Student o1, Student o2) {
16                         return o1.getName().compareTo(o2.getName());
17                     }
18                 });
19         students.add(student1);
20         students.add(student2);
21         students.add(student3);
22         System.out.println(students);
23     }
24 }
複製程式碼

 總結:

TreeSet容器中的物件要能排序,兩種實現排序方法:

1.TreeSet使用無參建構函式,容器中的物件實現Comparable介面,見改造一;

2.TreeSet構造時使用Comparator作為建構函式引數;

  比較方法如果返回0,則物件不能重複加入。

TreeSet底層是TreeMap