zoukankan      html  css  js  c++  java
  • Java中List的排序方法

    方法一:实现Comparable接口

    package com.java.test;
    
    public class Person implements Comparable<Person> {
    
        private String name;
        private Integer age;
    
        public Person() {
        }
    
        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public int compareTo(Person o) {
            return age.compareTo(o.getAge());//正序
         //return o.getAge().compareTo(age);//倒序
    } }
    package com.java.test;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class PersonTest {
    
        public static void main(String[] args) {
            List<Person> list = new ArrayList<Person>();
            list.add(new Person("E", 16));
            list.add(new Person("C", 14));
            list.add(new Person("A", 12));
            list.add(new Person("B", 13));
            list.add(new Person("D", 15));
            for (Person person : list) {
                System.out.println(person.getAge());
            }
            System.out.println("####################");
            Collections.sort(list);
            for (Person person : list) {
                System.out.println(person.getAge());
            }
    
        }
    }

    方法二:调用Collections.sort(List<T> list, Comparator<? super T> c),定义Comparator

    package com.java.test;
    
    public class Person {
    
        private String name;
        private Integer age;
    
        public Person() {
        }
    
        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
    }
    package com.java.test;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class PersonTest {
    
        public static void main(String[] args) {
            List<Person> list = new ArrayList<Person>();
            list.add(new Person("E", 16));
            list.add(new Person("C", 14));
            list.add(new Person("A", 12));
            list.add(new Person("B", 13));
            list.add(new Person("D", 15));
            for (Person person : list) {
                System.out.println(person.getAge());
            }
            System.out.println("####################");
            Collections.sort(list, new Comparator<Person>() {
                @Override
                public int compare(Person o1, Person o2) {
                    return o1.getAge().compareTo(o2.getAge());//正序
             //return o2.getAge().compareTo(o1.getAge());//倒序 } });
    for (Person person : list) { System.out.println(person.getAge()); } } }
  • 相关阅读:
    UVa 820 因特网带宽(最大流)
    UVa 1001 奶酪里的老鼠(Dijkstra或Floyd)
    UVa 821 网页跳跃(Floyd)
    UVa 11624 大火蔓延的迷宫
    UVa 10881 蚂蚁
    UVa 11300 分金币
    UVa 11729 突击战
    《额尔古纳河右岸》读书笔记
    HDU 1083 Courses(二分图匹配模板)
    UVa 10618 跳舞机
  • 原文地址:https://www.cnblogs.com/liaojie970/p/4806661.html
Copyright © 2011-2022 走看看