zoukankan      html  css  js  c++  java
  • ht-7 treeSet特性

    TreeSet
    TreeSet可以对set集合中的元素进行排序,默认按照asic码表的自然顺序排序,之所以treeset能排序是因为底层是二叉树,数据越多越慢,TreeSet是依靠TreeMap来实现的
    像TreeSet中存储自定义对象需要实现comparable接口。

     1 package com.iotek.set;
     2 
     3 import java.util.Iterator;
     4 import java.util.TreeSet;
     5 
     6 public class TreeSetDemo1 {
     7 
     8     public static void main(String[] args) {
     9 
    10         TreeSet<Personb> pset = new TreeSet<Personb>();
    11         pset.add(new Personb("chenchen", 30));
    12         pset.add(new Personb("lisi", 20));
    13         pset.add(new Personb("wangwu", 10));
    14         pset.add(new Personb("rose", 40));
    15         pset.add(new Personb("rose", 41));
    16         System.out.println(pset);
    17         Iterator<Personb> it = pset.iterator(); //
    18         while (it.hasNext()) { // 判断容器中有没有被迭代的数据
    19             Personb p = it.next(); // 取出来的是个Person类型
    20             System.out.println(p.getName() + "--" + p.getAge());
    21         }
    22 
    23         /*
    24          * 调用TreeSet类的对象pset的iterator()方法,返回一个迭代器对象, 使用这个迭代器对象
    25          */
    26 
    27         /*
    28          * (1)当Person类不实现Comparable接口时,System.out.println(pset)这条语句会报错:
    29          * Exception in thread "main"java.lang.ClassCastException:
    30          * com.iotek.set.Person cannot be cast to java.lang.Comparable
    31          * 这是因为,TreeSet和TreeMap一样,都是按照键的自然顺序来升序排列,
    32          * 对象无法做自然升序排列,应该要告诉TreeSet按照什么规则来排序
    33          * (2)如何对TreeSet对象的add()方法中添加的对象进行排序?需要这个对象的类
    34          * 来实现Comparable接口,在接口中重写compareTo()方法,来声明比比较规则
    35          */
    36     }
    37 
    38 }
    39 
    40 /*
    41  * 当调用TreeSet的无参数构造方法,其内部创建了一个TreeMap对象 public TreeSet() { this(new
    42  * TreeMap<E,Object>()); }
    43  */
    44 class Personb implements Comparable<Personb> {
    45     private String name;
    46     private int age;
    47 
    48     public Personb(String name, int age) {
    49         super();
    50         this.name = name;
    51         this.age = age;
    52         // this.age是Person类的实例化对象的age
    53     }
    54 
    55     public String getName() {
    56         return name;
    57     }
    58 
    59     public void setName(String name) {
    60         this.name = name;
    61     }
    62 
    63     public int getAge() {
    64         return age;
    65     }
    66 
    67     public void setAge(int age) {
    68         this.age = age;
    69     }
    70 
    71     @Override
    72     public int compareTo(Personb o) {
    73         if (this.age - o.getAge() > 0) {
    74             return 1;
    75         } else if (this.age - o.getAge() < 0) {
    76             return -1;
    77         }
    78         return 0;
    79     }
    80 
    81     @Override
    82     public String toString() {
    83         return "Personb [name=" + name + ", age=" + age + "]";
    84     }
    85     
    86 
    87 }

    TreeSet及常用API
    (1)TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序存储,访问和检索很快
    (2)在存储了大量的需要进行快速检索的排序信息的情况下,TreeSet是一个很好的选择
    (3)构造方法
    TreeSet()
    TreeSet(Collection c)
    TreeSet(Comparator comp)
    TreeSet(SortedSet ss)

    (4)总结 :TreeSet的内部操作的底层数据是TreeMap,只是我们操作的是TreeMap的Key
    TreeMap的Key是默认按照自然顺序升序排列的

  • 相关阅读:
    04邻接表深度和广度遍历DFS_BFS
    03邻接矩阵的深度和广度遍历的C语言实现
    02邻接表创建的C语言实现
    01邻接矩阵的创建C语言实现
    GUN的相关使用
    JAVA学习笔记
    排序的C语言实现
    线索二叉树的C语言实现
    maven配置logback
    多线程概念
  • 原文地址:https://www.cnblogs.com/enjoyjava/p/9393345.html
Copyright © 2011-2022 走看看