zoukankan      html  css  js  c++  java
  • list集合排序2

    java根据List内对象的属性排序

    原创 2016年12月07日 00:20:01

    方法一:实现Comparator接口,并重写compare方法

    • 实体类代码:
    import java.util.Comparator;
    
    /**
     * 学生类   方法一
     * 实现Comparator接口
     * 并重写compare方法
     * @author liaot
     *
     */
    public class Student implements Comparator<Student>{
        private String name; //姓名
        private int age;  //年龄
    
        //重写 比较方法  本次例子定义为按年龄比较
        @Override
        public int compare(Student o1, Student o2) {
            if(o1.getAge() > o2.getAge()){
                return 1;
            }else{
                return -1;
            }
        }
    
    
        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;
        }
    }
    • 测试类:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class Main {
    
        public static void main(String[] args) {
            //初始化四个不同的学生
            Student stu1 = new Student("路人甲", 20);
            Student stu2 = new Student("路人已", 18);
            Student stu3 = new Student("路人丙", 16);
            Student stu4 = new Student("路人丁", 19);
            //新建List把学生加进List
            List<Student> stuList = new ArrayList<>();
            stuList.add(stu1);
            stuList.add(stu2);
            stuList.add(stu3);
            stuList.add(stu4);
            System.out.println("排序前:=====");
            for(Student stu :stuList){
                System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
            }
            //排序
            Collections.sort(stuList, stu1);  //第一个参数为List 第二个参数为对象的一个实例
            System.out.println("排序后:=====");
            for(Student stu :stuList){
                System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
            }
        }
    
    }
    • 运行结果: 
      这里写图片描述

    方法二:实现Comparable接口 并重写compareTo方法

    /**
     * 学生类 方法二  实现Comparable接口 并重写compareTo方法
     * 
     * @author liaot
     *
     */
    public class Student2 implements Comparable<Student2> {
        private String name; // 姓名
        private int age; // 年龄
    
        // 重写 比较方法 本次例子定义为按年龄比较
        @Override
        public int compareTo(Student2 stu) {
            if (this.age > stu.getAge()) {
                return 1;
            } else {
                return -1;
            }
        }
    
        public Student2(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;
        }
    
    }
    • 测试类
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class Main2 {
    
        public static void main(String[] args) {
            //初始化四个不同的学生
            Student2 stu1 = new Student2("路人甲", 20);
            Student2 stu2 = new Student2("路人已", 18);
            Student2 stu3 = new Student2("路人丙", 16);
            Student2 stu4 = new Student2("路人丁", 19);
            //新建List把学生加进List
            List<Student2> stuList = new ArrayList<>();
            stuList.add(stu1);
            stuList.add(stu2);
            stuList.add(stu3);
            stuList.add(stu4);
            System.out.println("排序前:=====");
            for(Student2 stu :stuList){
                System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
            }
            //排序
            Collections.sort(stuList);  //只有一个参数参数为List
            System.out.println("排序后:=====");
            for(Student2 stu :stuList){
                System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
            }
        }
    
    }
    • 运行结果 
      这里写图片描述

    三、总结:两种方式写法和用法上的区别: 

    这里写图片描述

    版权声明:欢迎转载,请注明原文地址:http://blog.csdn.net/c1481118216 http://blog.csdn.net/c1481118216/article/details/53496083,http://blog.csdn.net/c1481118216/article/details/53496083
     
     
  • 相关阅读:
    Group_concat介绍与例子
    国家大学科技园名单
    shell截取第五行第三列
    (Deep) Neural Networks (Deep Learning) , NLP and Text Mining
    28款GitHub最流行的开源机器学习项目,推荐GitHub上10 个开源深度学习框架
    SpeeDO —— 并行深度学习系统
    BrandZ:2016年全球最具价值品牌百强榜(完整报告)
    jdk1.7升级到jdk1.8后出错: [ERROR] javadoc: warning
    伪基站收集方圆500至1000米范围内的手机卡信息,并且通过伪基站设备伪装成银行、中国移动、中国联通等任意号码向手机用户发送短信或拨打电话
    360手机助手内部资料曝光,63张PPT纯干货
  • 原文地址:https://www.cnblogs.com/zxwBj/p/8602986.html
Copyright © 2011-2022 走看看