zoukankan      html  css  js  c++  java
  • Java中对List集合排序的两种方法

    第一种方法,就是list中对象实现Comparable接口,代码如下:

    public class Person implements Comparable<Person> {
        private String name;
        private Integer order;
     
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
     
        /**
         * @param name
         *            the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
     
        /**
         * @return the order
         */
        public Integer getOrder() {
            return order;
        }
     
        /**
         * @param order
         *            the order to set
         */
        public void setOrder(Integer order) {
            this.order = order;
        }
     
        @Override
        public int compareTo(Person arg0) {
            return this.getOrder().compareTo(arg0.getOrder());
        }
     
    }
    View Code
    public static void main(String[] args) {
        List<Person> listA = new ArrayList<Person>();
        Person p1 = new Person();
        Person p2 = new Person();
        Person p3 = new Person();
     
        p1.setName("name1");
        p1.setOrder(1);
        p2.setName("name2");
        p2.setOrder(2);
        p3.setName("name3");
        p3.setOrder(3);
     
        listA.add(p2);
        listA.add(p1);
        listA.add(p3);
        Collections.sort(listA);
        for (Person p : listA) {
            System.out.println(p.getName());
        }
    }
    View Code

    第二种方法,就是在重载Collections.sort方法,代码如下:

    public class Person {
        private String name;
        private Integer order;
     
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
     
        /**
         * @param name
         *            the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
     
        /**
         * @return the order
         */
        public Integer getOrder() {
            return order;
        }
     
        /**
         * @param order
         *            the order to set
         */
        public void setOrder(Integer order) {
            this.order = order;
        }
     
    }
    View Code
    public static void main(String[] args) {
            List<Person> listA = new ArrayList<Person>();
            Person p1 = new Person();
            Person p2 = new Person();
            Person p3 = new Person();
     
            p1.setName("name1");
            p1.setOrder(1);
            p2.setName("name2");
            p2.setOrder(2);
            p3.setName("name3");
            p3.setOrder(3);
     
            listA.add(p2);
            listA.add(p1);
            listA.add(p3);
             
            Collections.sort(listA, new Comparator<Person>() {
                public int compare(Person arg0, Person arg1) {
                    return arg0.getOrder().compareTo(arg1.getOrder());
                }
            });
             
            for (Person p : listA) {
                System.out.println(p.getName());
            }
        }
    View Code

    两次执行的结果都是:

    name1

    name2

    name3

  • 相关阅读:
    STM32 IAP程序 源码 和测试代码 有详细的中文注释
    mysql读写分离配置,利用mybatis实现,解释为什么dynamicDataSource不行
    mysql主从复制的配置总结
    Chapter 2 Open Book——7
    leetcode415---字符串大数相加
    Chapter 2 Open Book——6
    leetcode83,删除有序链表中的重复元素
    Chapter 2 Open Book——5
    Chapter 2 Open Book——4
    leetcode24,交换链表相邻的节点
  • 原文地址:https://www.cnblogs.com/longshiyVip/p/5102231.html
Copyright © 2011-2022 走看看