• TreeSet的自然排序(自定义对象 compareTo方法)


    》要实现自然排序,对象集合必须实现Comparable接口,并重写compareTo()方法

    》一般需求中描述的是“主要条件”,如:按姓名长度排序。  需注意次要条件 如:长度相同时,姓名内容,年龄等条件是否相等,这决定着是否存入TreeSet集合。

     

    package cn.itcast.day21.treeset;
    /*
     * 要实现自然排序,就一定要实现Comparable接口,并重写compareTo()方法
     * 
     * 若不实现Comparable接口,而把对象往TreeSet中添加,就会报ClassCastException
     *       原因:java.util.TreeMap.put(TreeMap.java:542) 
     *      Comparable<? super K> k = (Comparable<? super K>) key;//key=集合元素,被强行转换成接口类型 
     * 
     */
    public class Student implements Comparable<Student>{
        private String name;
        private int age;
        
        public Student() {
            super();
        }
        public Student(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        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;
        }
        
        @Override
        public int compareTo(Student o) {
            int num=this.getName().length()-o.getName().length();
            int num2=num==0?this.getName().compareTo(o.getName()):num;
            int num3=num2==0?this.getAge()-o.getAge():num2;
            
            return num3;
        }
    }

     

     

    package cn.itcast.day21.treeset;
    
    import java.util.TreeSet;
    
    /*
     * TreeSet 按照自定义对象Student的姓名长度自然排序
     * 
     * 
     * 分析:
     *         A:实现自然排序,元素的类就要实现Comparable接口,并重写compareTo()方法
     *         B:主要条件 姓名长度
     *         C:次要条件  姓名内容,年龄
     */
    public class TreeSetDemo {
        public static void main(String[] args) {
            //创建集合对象
            TreeSet<Student> ts=new TreeSet<Student>();
            //创建元素对象
            Student s1=new Student("linqingxia",27);
            Student s2=new Student("wuqilong",27);
            Student s3=new Student("wanglihong",34);
            Student s4=new Student("zhouxingchi",57);
            Student s5=new Student("linqingxia",28);
            Student s6=new Student("linqingxia",27);
            
            //添加集合元素
            ts.add(s1);
            ts.add(s2);
            ts.add(s3);
            ts.add(s4);
            ts.add(s5);
            ts.add(s6);
            
            //遍历集合 
            for(Student s:ts){
                System.out.println(s.getName()+"-----"+s.getAge());
            }
        }
    
    }
    /** 运行结果:
    wuqilong-----27
    linqingxia-----27
    linqingxia-----28
    wanglihong-----34
    zhouxingchi-----57
    */
  • 相关阅读:
    Educational Codeforces Round 64 (Rated for Div. 2)题解
    Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle
    莫队算法总结
    cobalt strike使用笔记
    CMD命令行下载文件
    CTF线下赛AWD模式下的生存技巧
    python中multiprocessing模块
    密码重置
    python中的argparse模块
    python中BeautifulSoup模块
  • 原文地址:https://www.cnblogs.com/qq-757617012/p/4287395.html
Copyright © 2020-2023  润新知