• Java集合的排序


    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    import org.junit.Test;
    
    public class StackDome {
    
        
        @Test
        public void sortTest() {
            /**
             * 单条件排序
             */
            List<User> users = new CopyOnWriteArrayList<User>();
            int count = 0;
            final int IDS = 10000;
            do {
                // 随机生产1到100范围的随机数
                int random = (int) (1 + Math.random() * (100 - 1 + 1));
                int randomId = (int) (1 + Math.random() * (11 - 1 + 1));
                users.add(new User(String.valueOf("sound" + count), random, IDS + randomId));
                count++;
            } while (count < 10);
    
            // 排序前,单独获取年龄
            // Set<Integer> userAges =
            // users.stream().map(User::getUserAge).collect(Collectors.toSet());
            // userAges.forEach(s->{System.out.println(s);});
            users.forEach(s -> {
                System.out.println("排序前====》名字" + s.getUserName() + ",年龄:" + s.getUserAge()+","+s.getId());
            });
            // 按照年龄进行排序。从小到大{升序}
            Collections.sort(users, new Comparator<User>() {
                @Override
                public int compare(User o1, User o2) {
                    int compare = Integer.compare(o1.getUserAge(), o2.getUserAge());
                    return compare;
                }
            });
            // 排序后
            users.forEach(s -> {
                System.err.println("排序后====》名字" + s.getUserName() + ",年龄:" + s.getUserAge()+","+s.getId());
            });
             //
            //补充:
            /**
             * 多条件排序是根据优先级去判断,如果年龄相等,则再根据ID去比较 这里用年龄作为优先级,
             */
            Collections.sort(users, new Comparator<User>() {
                @Override
                public int compare(User o1, User o2) {
                    int compare = Integer.compare(o1.getUserAge(), o2.getUserAge());
                    if (compare == 0) {
                        compare = Integer.compare(o1.getId(), o2.getId());
                    }
                    return compare;
                }
            });
            users.forEach(s -> {
                System.out.println("双条件排序====》名字" + s.getUserName() + ",年龄:" + s.getUserAge()+","+s.getId());
            });
        }
    }
    class User {
        private int userAge;
        private String userName;
        private int id;
    
        public User(String userName, int userAge, int id) {
            this.userName = userName;
            this.userAge = userAge;
            this.id = id;
        }
    
        public int getUserAge() {
            return userAge;
        }
    
        public void setUserAge(int userAge) {
            this.userAge = userAge;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
    }
    
    运行效果:
    排序前====》名字sound0,年龄:96,10003
    排序前====》名字sound1,年龄:58,10001
    排序前====》名字sound2,年龄:59,10007
    排序前====》名字sound3,年龄:46,10004
    排序前====》名字sound4,年龄:6,10011
    排序前====》名字sound5,年龄:61,10004
    排序前====》名字sound6,年龄:24,10001
    排序前====》名字sound7,年龄:24,10007
    排序前====》名字sound8,年龄:10,10006
    排序前====》名字sound9,年龄:47,10008
    排序后====》名字sound4,年龄:6,10011
    排序后====》名字sound8,年龄:10,10006
    排序后====》名字sound6,年龄:24,10001
    排序后====》名字sound7,年龄:24,10007
    排序后====》名字sound3,年龄:46,10004
    排序后====》名字sound9,年龄:47,10008
    排序后====》名字sound1,年龄:58,10001
    排序后====》名字sound2,年龄:59,10007
    排序后====》名字sound5,年龄:61,10004
    排序后====》名字sound0,年龄:96,10003
    双条件排序====》名字sound4,年龄:6,10011
    双条件排序====》名字sound8,年龄:10,10006
    双条件排序====》名字sound6,年龄:24,10001
    双条件排序====》名字sound7,年龄:24,10007
    双条件排序====》名字sound3,年龄:46,10004
    双条件排序====》名字sound9,年龄:47,10008
    双条件排序====》名字sound1,年龄:58,10001
    双条件排序====》名字sound2,年龄:59,10007
    双条件排序====》名字sound5,年龄:61,10004
    双条件排序====》名字sound0,年龄:96,10003
  • 相关阅读:
    LeetCode 第 193 场周赛
    LeetCode 每日一题 15. 三数之和
    LeetCode 每日一题 739. 每日温度
    [转]邹承鲁院士写他是如何读文献
    LeetCode 每日一题 面试题46. 把数字翻译成字符串
    LeetCode 每日一题 990. 等式方程的可满足性
    传说中编程界的龙书、虎书、鲸书、魔法书…… 指的都是哪些?
    LeetCode 每日一题 238. 除自身以外数组的乘积
    C++ 关键字 explicit
    LeetCode 每日一题 837. 新21点
  • 原文地址:https://www.cnblogs.com/mature1021/p/10245382.html
Copyright © 2020-2023  润新知