• 【Collections:集合工具类:自然排序和比较器排序】


    package com.yjf.esupplier.common.test;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    /**
     * @author shusheng
     * @description 自然排序和比较器排序(对象)
     * @Email shusheng@yiji.com
     * @date 2018/12/18 17:11
     */
    public class CollectionsDemo1 {
    
        public static void main(String[] args) {
            // 创建集合对象
            List<Student> list = new ArrayList<Student>();
    
            // 创建学生对象
            Student s1 = new Student("林青霞", 27);
            Student s2 = new Student("风清扬", 30);
            Student s3 = new Student("刘晓曲", 28);
            Student s4 = new Student("武鑫", 29);
            Student s5 = new Student("林青霞", 27);
    
            // 添加元素对象
            list.add(s1);
            list.add(s2);
            list.add(s3);
            list.add(s4);
            list.add(s5);
    
            // 排序
            // 自然排序(须比较对象实现Comparable接口)
            Collections.sort(list);
            // 遍历集合
            for (Student s : list) {
                System.out.println(s.getName() + "---" + s.getAge());
            }
            System.out.println("---------------------------------------");
            // 比较器排序
            // 如果同时有自然排序和比较器排序,以比较器排序为主
            Collections.sort(list, new Comparator<Student>() {
                @Override
                public int compare(Student s1, Student s2) {
                    int num = s2.getAge() - s1.getAge();
                    int num2 = num == 0 ? s1.getName().compareTo(s2.getName())
                            : num;
                    return num2;
                }
            });
    
            // 遍历集合
            for (Student s : list) {
                System.out.println(s.getName() + "---" + s.getAge());
            }
    
        }
    
    }
    
    class Student implements Comparable<Student>{
        private String name;
        private int age;
    
        public Student(String name, int age) {
            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 s) {
            int num = this.age - s.age;
            int num2 = num == 0 ? this.name.compareTo(s.name)
                    : num;
            return num2;
        }
    }
    终身学习者
  • 相关阅读:
    oracle学习 五 使用存储过程创建一个重置密码为123456的功能(持续更新中)
    oracle学习 四(持续更新中)无法为表空间 MAXDATA 中的段创建 INITIAL 区
    P3224 [HNOI2012]永无乡
    P3521 [POI2011]ROT-Tree Rotations
    UVA11090 Going in Cycle!!
    P1136 迎接仪式
    P1984 [SDOI2008]烧水问题(具体证明)
    P1494 [国家集训队]小Z的袜子
    P2680 运输计划
    P2831 愤怒的小鸟
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/10341226.html
Copyright © 2020-2023  润新知