zoukankan      html  css  js  c++  java
  • TreeSet集合

    TreeSet集合特点:

    1.无序,不允许重复
    2.底层使用的数据结构是二叉树
    3.TreeSet集合一定要实现一种排序 否则就会 ClassCastException
    4.有两种排序:自然排序和定制排序

    TreeSet遍历:

    前序:中左右
    中序:左中右
    后序:左右中

    TreeSet添加元素原则

    左小右大

    Comparable和Comparator区别

    1,Comparable是直接在创建对象的类中实现
    2,2,Comparator是建一个单独的比较器类来实现,或使用匿名内部类。将new出比较器的对象传进去【TreeSet list=new TreeSet<>(比较器对象)】

    两种排序的用法:

    1. TreeSet集合排序方式一:自然排序Comparable
      http://blog.csdn.net/baidu_37107022/article/details/70207564

    2. TreeSet集合排序方式二:定制排序Comparator
      http://blog.csdn.net/baidu_37107022/article/details/70207633

    练习:
    TreeSet集合中放的多个人的对象 , Person类 有属性 name,age,sex
    排序规则: 第一条件 年龄降序,第二条件 姓名 降序,第三条件 性别升序

    Person类:

    import java.text.CollationKey;
    import java.text.Collator;
    
    public class Person implements Comparable<Person> {
        private String name;
        private int age;
        private String sex;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
        }
        public Person(String name, int age, String sex) {
            super();
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        public Person() {
            super();
        }
        @Override
        public int compareTo(Person o) {
            //排序规则: 第一条件  年龄降序,第二条件  姓名 降序,第三条件 性别升序
            //比较结果大于0时,return 1,则为升序;若return -1,则会降序;
            //第一条件  年龄降序
            if(this.age>o.age){
                return -1;
            }else if (this.age<o.age) {
                return 1;
            }else {
                //第二条件  姓名 降序     
                //注意:非英语类语言是不能直接排序的,需要做一下转换后才能排序
                CollationKey key=Collator.getInstance().getCollationKey(this.name);
                CollationKey key2=Collator.getInstance().getCollationKey(o.name);
                int num=key.compareTo(key2);
                if(num>0){
                    return -1;
                }else if (num<0) {
                    return 1;
                }else {
                    //第三条件 性别升序
                    CollationKey key3=Collator.getInstance().getCollationKey(this.sex);
                    CollationKey key4=Collator.getInstance().getCollationKey(o.sex);
                    return key3.compareTo(key4);
                }
            }
        }
    
    }
    

    测试:

    import java.util.TreeSet;
    
    public class Test {
        public static void main(String[] args) {
            TreeSet<Person> set=new TreeSet<>();
            //第一条件  年龄降序,第二条件  姓名 降序,第三条件 性别升序
            set.add(new Person("李白", 28, "男"));
            set.add(new Person("李白", 80, "女"));
            set.add(new Person("战三", 80, "女"));
            set.add(new Person("战三", 80, "男"));
            for (Person person : set) {
                System.out.println(person);
            }
        }
    }
    

    结果:
    这里写图片描述

  • 相关阅读:
    vector详解
    浅谈 莫斯电码&栅栏密码
    牛牛的BRD迷宫2 构造+思维
    Codeforces Round #409 C. Voltage Keepsake(二分+思维)
    hdu 2609 How many(最小表示法)
    hdu 4513(Manacher)
    codeforces 486 E. LIS of Sequence(dp)
    codeforces 486 D. Valid Sets(树形dp)
    hdu3746(kmp最小循环节)
    poj 2406 Power Strings(kmp next的应用)
  • 原文地址:https://www.cnblogs.com/TCB-Java/p/6770135.html
Copyright © 2011-2022 走看看