zoukankan      html  css  js  c++  java
  • Java list对象列表排序 实例

    package com.test;
    
    public class Bean {
    
        private String name;
        private int priority;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getPriority() {
            return priority;
        }
    
        public void setPriority(int priority) {
            this.priority = priority;
        }
    
    }
    package com.test;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Random;
    
    public class Test {
    
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public static void main(String[] args) {
    
            List<Bean> list = new ArrayList<Bean>();
            for (int i = 1; i < 10; i++) {
                Bean bean = new Bean();
                bean.setName("name_" + i);
                bean.setPriority(new Random().nextInt(10));
                list.add(bean);
            }
            // 打印
            for (Bean b : list) {
                System.out.println(b.getPriority() + "|" + b.getName());
            }
    
            Collections.sort(list, new Comparator() {
                public int compare(Object a, Object b) {
                    int one = ((Bean) a).getPriority();
                    int two = ((Bean) b).getPriority();
                    return one - two;
                }
            });
            System.out.println("--------------------------------");
            // 打印
            for (Bean b : list) {
                System.out.println(b.getPriority() + "|" + b.getName());
            }
        }
    
    }
  • 相关阅读:
    每日总结6.14
    每日总结6.13
    每日总结6.12
    每日总结6.11
    用户故事与敏捷方法阅读笔记4
    用户故事与敏捷方法阅读笔记3
    团队冲刺第一阶段燃尽图
    团队冲刺10
    智能物联网:将人工智能的力量带入物联网
    MyEclipse修改文件后Building workspace时间过长
  • 原文地址:https://www.cnblogs.com/qqzy168/p/4098031.html
Copyright © 2011-2022 走看看