• 排序容器——高淇JAVA300讲笔记之TreeSet与TreeMap


    • TreeMap:确保key可以排序或者提供比较器
      • 确保key可以排序
      • 提供key比较器  

                     public TreeMap(Comparator<?super K> comparator)

    • TreeSet:
      • 确保元素实体可以排序
      • 排序比较器  

                     public TreeSet(Comparator<?super E> comparator)

    1.TreeSet数据元素可以排序且不可重复

    对比:Set接口:HashSet,元素必须重写hashCode和equals方法。

    去重:比较等于0即重复。

    1)、元素可以排序 java.lang.Comparable接口 + compareTo方法

    new TreeSet()

    2)、排序业务类 java.util.Comparator接口 + compare方法

    new TreeSet(Comparator<? super E> comparator)

    注意:在添加数据时排序,数据更改不会影响原来的顺序。所以不要修改数据,否则可能重复

    下面来看例子。

    TreeSet案例一:实现java.util.Comparator接口

    先建一个Person类,里面包含姓名、帅气指数。

     1 package com.bjsxt.sort.col;
     2 
     3 public class Person {
     4     private final String name;//名称,这里加上final,是为了保证值不能被修改,以免在set里发生值重复的情况
     5     private final int handsome;//帅气指数
     6     
     7     //无参构造器
     8     public Person() {
     9         super();
    10         name = null;
    11         handsome = 0;
    12     }
    13 
    14     //有参构造器
    15     public Person(String name, int handsome) {
    16         super();
    17         this.name = name;
    18         this.handsome = handsome;
    19     }
    20 
    21     public String getName() {
    22         return name;
    23     }
    24 
    25     public int getHandsome() {
    26         return handsome;
    27     }
    28 
    29     @Override
    30     public String toString() {
    31         return "姓名:"+this.name+",帅气指数"+this.handsome+"
    ";
    32     }
    33     
    34     
    35 }

    然后利用业务排序类进行排序。注意,对象是在存放到TreeSet容器内的过程中进行排序的,且数据更改不会影响原来的顺序。所以不要修改数据,否则可能重复。

     1 package com.bjsxt.sort.col;
     2 
     3 import java.util.TreeSet;
     4 
     5 /**
     6  * 提供了解耦的方式:业务排序类
     7  *
     8  */
     9 public class TreeSetDemo {
    10 
    11     public static void main(String[] args) {
    12         // TODO Auto-generated method stub
    13         Person p1 = new Person("您",100);
    14         Person p2 = new Person("刘德华",1000);
    15         Person p3 = new Person("梁朝伟",1200);
    16         Person p4 = new Person("老裴",50);
    17         
    18         //依次存放到Tree容器中,使用排序的业务类(匿名内部类)
    19         TreeSet<Person> persons = new TreeSet<Person>(
    20                     new java.util.Comparator<Person>() {
    21 
    22                         @Override
    23                         public int compare(Person o1, Person o2) {
    24                             return o1.getHandsome() - o2.getHandsome();
    25                         }
    26                         
    27                     }
    28                 );
    29         persons.add(p1);
    30         //TreeSet 在添加数据时排序
    31         persons.add(p2);
    32         persons.add(p3);
    33         persons.add(p4);
    34         
    35         System.out.println(persons);
    36         /*
    37         //改变数据
    38         p4.setHandsome(100);
    39         p4.setName("您");
    40         */
    41         //p4与p1内容重复
    42          
    43         //System.out.println(persons);
    44         
    45     }
    46 
    47 }

    运行结果:按帅气值升序。

    [姓名:老裴,帅气指数50
    , 姓名:您,帅气指数100
    , 姓名:刘德华,帅气指数1000
    , 姓名:梁朝伟,帅气指数1200
    ]

    TreeSet案例二:实现 java.lang.Comparable接口

    先建一个工人类Worker,这个实体类实现了java.lang.Comparable接口,并且重写了compareTo方法,按工资升序。

     1 package com.bjsxt.sort.col;
     2 
     3 public class Worker implements java.lang.Comparable<Worker>{
     4     //工种
     5     private String type;
     6     //工资
     7     private double salary;
     8     
     9     //无参构造器
    10     public Worker() {
    11         super();
    12     }
    13     
    14     //带参构造器
    15     public Worker(String type, double salary) {
    16         super();
    17         this.type = type;
    18         this.salary = salary;
    19     }
    20 
    21     public String getType() {
    22         return type;
    23     }
    24     public void setType(String type) {
    25         this.type = type;
    26     }
    27     public double getSalary() {
    28         return salary;
    29     }
    30     public void setSalary(double salary) {
    31         this.salary = salary;
    32     }
    33 
    34     /**
    35      * 按工资升序
    36      */
    37     @Override
    38     public int compareTo(Worker o) {
    39         return this.salary>o.salary?1:(this.salary==o.salary?0:-1);
    40     }
    41     
    42     @Override
    43     public String toString() {
    44         return "工种:" + this.type + ",工资:" + this.salary + "
    ";
    45     }
    46     
    47 }

    然后将对象存入TreeSet,在存入时进行排序。

     1 package com.bjsxt.sort.col;
     2 
     3 import java.util.TreeSet;
     4 
     5 /**
     6  * 实体类实现Comparable 接口的应用
     7  *
     8  */
     9 public class TreeSetDemo02 {
    10     public static void main(String[] args) {
    11         Worker w1 = new Worker("垃圾回收员",12000);
    12         Worker w2 = new Worker("农名工",8000);
    13         Worker w3 = new Worker("程序猿",5000);
    14     
    15         TreeSet<Worker> employees = new TreeSet<Worker>();
    16         employees.add(w1);
    17         employees.add(w2);
    18         employees.add(w3);
    19         System.out.println(employees);
    20     }
    21 }

    运行结果:按工资升序。

    [工种:程序猿,工资:5000.0
    , 工种:农名工,工资:8000.0
    , 工种:垃圾回收员,工资:12000.0
    ]

    2.TreeMap:要求键可以排序,与上述TreeSet同理

    TreeMap案例1:实现 java.util.Comparator接口

    Person类同上,这里就不重复贴了。

    下面是排序代码。

     1 package com.bjsxt.sort.col;
     2 
     3 import java.util.Set;
     4 import java.util.TreeMap;
     5 
     6 public class TreeMapDemo {
     7     public static void main(String[] args) {
     8         Person p1 = new Person("您",100);
     9         Person p2 = new Person("刘德华",1000);
    10         Person p3 = new Person("梁朝伟",1200);
    11         Person p4 = new Person("老裴",50);
    12         
    13         TreeMap<Person,String> map = new TreeMap<Person,String>(
    14                     new java.util.Comparator<Person>() {
    15     
    16                         @Override
    17                         public int compare(Person o1, Person o2) {
    18                             return o1.getHandsome() - o2.getHandsome();
    19                         }
    20                         
    21                     }
    22                 );
    23         map.put(p1, "bjsxt");
    24         map.put(p2, "bjsxt");
    25         map.put(p3, "bjsxt");
    26         map.put(p4, "bjsxt");
    27         
    28         //查看键
    29         Set<Person> persons = map.keySet();
    30         System.out.println(persons);
    31         
    32     }
    33 }

    运行结果:按照帅气值升序。

    [姓名:老裴,帅气指数50
    , 姓名:您,帅气指数100
    , 姓名:刘德华,帅气指数1000
    , 姓名:梁朝伟,帅气指数1200
    ]

    TreeMap案例2:实现了java.lang.Comparable接口

    Worker类同上,这里就不重复贴了。

    下面是排序代码。

     1 package com.bjsxt.sort.col;
     2 
     3 import java.util.TreeMap;
     4 
     5 public class TreeMapDemo02 {
     6     public static void main(String[] args) {
     7         Worker w1 = new Worker("垃圾回收员",12000);
     8         Worker w2 = new Worker("农名工",8000);
     9         Worker w3 = new Worker("程序猿",5000);
    10     
    11         TreeMap<Worker,String> employees = new TreeMap<Worker,String>();
    12         employees.put(w1,"bjsxt");
    13         employees.put(w2,"bjsxt");
    14         employees.put(w3,"bjsxt");
    15         System.out.println(employees.keySet());
    16     }
    17 }

    运行结果:

    [工种:程序猿,工资:5000.0
    , 工种:农名工,工资:8000.0
    , 工种:垃圾回收员,工资:12000.0
    ]
  • 相关阅读:
    SCM Introduction
    upper lower capitalize and title
    isinstance
    splitlines
    使用 UITableView 创建表格应用演练(4)——自定义单元格
    高性能网站的十四条黄金法则
    javascript设计模式继承(上)
    这些年,我收集的JavaScript代码(一)
    ObjectiveC语法之KVO的使用
    面试中的Singleton
  • 原文地址:https://www.cnblogs.com/swimminglover/p/8325155.html
Copyright © 2020-2023  润新知