• 【java】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 }
    Student
     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 }
    TreeSetDemo

     运行结果:

    原因: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 }
    Student
     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 };//类体最后一个大括号后有;不会报错
    TreeSetDemo

    改造二:用内部类实现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 }
    Student
     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 }
    TreeSetDemo

     改造三:用匿名类实现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 }
    Student
     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 }
    TreeSetDemo

     总结:

    TreeSet容器中的对象要能排序,两种实现排序方法:

    1.TreeSet使用无参构造函数,容器中的对象实现Comparable接口,见改造一;

    2.TreeSet构造时使用Comparator作为构造函数参数;

      比较方法如果返回0,则对象不能重复加入。

    TreeSet底层是TreeMap

  • 相关阅读:
    JavaScript日常学习6
    JavaScript日常学习5
    JavaScript日常学习4
    JavaScript日常学习3
    JavaScript日常学习2
    JavaScript日常学习1
    iOS开发之 Lottie -- 炫酷的动效
    Adobe After Effect CC2017 for Mac
    随机四则运算的程序
    关于四则运算进一步优化的构思
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/6623063.html
Copyright © 2020-2023  润新知