• List排序


    <pre name="code" class="java">package cn.edu.zzu;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class SortTest {
        
        public static void main(String[] args) {
            List<String> lists = new ArrayList<String>();
            List<A> list = new ArrayList<A>();
            List<B> listB = new ArrayList<B>();
            lists.add("5");
            lists.add("2");
            lists.add("9");
            //lists中的对象String 本身含有compareTo方法,所以可以直接调用sort方法,按自然顺序排序,即升序排序
            Collections.sort(lists);
            
            A aa = new A();
            aa.setName("aa");
            aa.setOrder(1);
            A bb = new A();
            bb.setName("bb");
            bb.setOrder(2);
            list.add(bb);
            list.add(aa);
            //list中的对象A实现Comparable接口
            Collections.sort(list);
            
            B ab = new B();
            ab.setName("ab");
            ab.setOrder("1");
            B ba = new B();
            ba.setName("ba");
            ba.setOrder("2");
            listB.add(ba);
            listB.add(ab);
            //根据Collections.sort重载方法来实现
            Collections.sort(listB,new Comparator<B>(){
                public int compare(B b1, B b2) {
                    return b1.getOrder().compareTo(b2.getOrder());
                }
                
            });
            
            System.out.println(lists);
            System.out.println(list);
            System.out.println(listB);
            
        }
    
    }
    
    class A implements Comparable<A>{
        private String name;
        private Integer order;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        public Integer getOrder() {
            return order;
        }
        public void setOrder(Integer order) {
            this.order = order;
        }
        @Override
        public String toString() {
            return "name is "+name+" order is "+order;
        }
        public int compareTo(A a) {
            return this.order.compareTo(a.getOrder());
        }
        
    }
    
    class B{
        private String name;
        private String order;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getOrder() {
            return order;
        }
        public void setOrder(String order) {
            this.order = order;
        }
        @Override
        public String toString() {
            return "name is "+name+" order is "+order;
        }
    }</pre><br>
    <pre></pre>
    <p>运行结果:</p>
    <p>[2, 34, 3434]<br>
    [name is aa order is 1, name is bb order is 2]<br>
    [name is ab order is 1, name is ba order is 2]<br>
    </p>
    <p></p><pre name="code" class="java">package cn.edu.zzu;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class List元素排序 {
        public static void main(String[] args) {
    
            List<Student> list = new ArrayList<Student>();
    
            // 创建3个学生对象,年龄分别是20、19、21,并将他们依次放入List中
            Student s1 = new Student();
            s1.setAge(20);
            Student s2 = new Student();
            s2.setAge(19);
            Student s3 = new Student();
            s3.setAge(21);
            list.add(s1);
            list.add(s2);
            list.add(s3);
    
            System.out.println("排序前:" + list);
    
            Collections.sort(list, new Comparator<Student>() {
    
                /*
                 * int compare(Student o1, Student o2) 返回一个基本类型的整型, 返回负数表示:o1 小于o2,
                 * 返回0 表示:o1和o2相等, 返回正数表示:o1大于o2。
                 */
                public int compare(Student o1, Student o2) {
    
                    // 按照学生的年龄进行升序排列
                    if (o1.getAge() > o2.getAge()) {
                        return 1;
                    }
                    if (o1.getAge() == o2.getAge()) {
                        return 0;
                    }
                    return -1;
                }
            });
            System.out.println("排序后:" + list);
        }
    }
    
    class Student {
    
        private int age;
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return getAge() + "";
        }
    }</pre><br>
    <br>
    <p></p>
    <p>排序前:[20, 19, 21]<br>
    排序后:[19, 20, 21]<br>
    </p>
    <p><br>
    </p>
    <p><br>
    </p>
    <p><br>
    </p>
       
  • 相关阅读:
    EntityFramework 数据库的迁移
    free之后将指针置为NULL
    结构体对齐及#pragma详细解释
    大端小端
    TCP封包解包---如有错误,请纠正!
    堆栈----好久没有更新了
    附近点搜索
    LRU Cache
    电商的秒杀和抢购
    搜索关键词智能提示suggestion
  • 原文地址:https://www.cnblogs.com/chenglc/p/8421619.html
Copyright © 2020-2023  润新知