TreeSet、Comparable、Comparator、内部类、匿名类

来源:互联网 发布:kvm centos虚拟化 编辑:程序博客网 时间:2024/06/13 22:49
复制代码
 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     @Override25     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     @Override25     public String toString() {26         return "Student [name=" + name + "]";27     }28     @Override29     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     @Override25     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         @Override21         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     @Override25     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                     @Override15                     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


转载自:http://www.cnblogs.com/xiongjiawei/p/6623063.html

阅读全文
0 0