• TreeSet多字段排序


    package st;
    
    public class PersonBean implements Comparable<Object>{
    
        private String name;    
        private int age;    
        
        PersonBean(String name, int age) {    
            this.name = name;    
            this.age = age;    
        }    
        
        public String getName() {    
            return name;    
        }    
        
        public int getAge() {    
            return age;    
        }    
        
        @Override    
        public int compareTo(Object obj) {    
            if (!(obj instanceof PersonBean))    
                throw new RuntimeException();    
            /**  
             * 在这里定义比较对象大小的方法  
             * 先进行年龄的比较,年龄不相等再进行姓名的比较  
             * 如果年龄与姓名都相等,那么就代表同一个对象  
             */    
            PersonBean pe = (PersonBean) obj;    
            if (this.age > pe.age)    
                return 1;    
            if (this.age == pe.age) {    
                return this.name.compareTo(pe.name);    
            }    
        
            return -1;    
        }    
    
    }
    import java.util.Iterator;
    import java.util.TreeSet;
    
    public class Dup {
            public static void main(String[] args) {    
            
                // 向这个集合中存储自定义对象    
                TreeSet<PersonBean> ts = new TreeSet<PersonBean>();    
                ts.add(new PersonBean("lishi", 22));    
                ts.add(new PersonBean("lishi1", 23));    
                ts.add(new PersonBean("lishi56", 25));    
                ts.add(new PersonBean("lishi4", 25));    
            
                //获取迭代器,取出其中的元素    
                Iterator<PersonBean> it = ts.iterator();    
                while (it.hasNext()) {    
                    PersonBean pe = (PersonBean) it.next();    
                    System.out.println(pe.getAge() + "  " + pe.getName());    
                }    
    
    }
    }
  • 相关阅读:
    HTTPS证书撤销
    前端跨域问题解析
    js递归遍历key
    阿里云centos配置nginx和nodejs
    Linux 查看服务器配置
    解决IPOD NANO7无法开机
    GIF添加3D加速
    关于bootstrap Modal弹窗 滚动条的问题
    Just-In-Time Debugging in Visual Studio 禁止VS在服务器上调试
    利用ASP.NET操作IIS (可以制作安装程序)
  • 原文地址:https://www.cnblogs.com/lhl-shubiao/p/9793191.html
Copyright © 2020-2023  润新知