您的位置:

Java编程中List排序的方法

一、sort方法排序

对于List集合,Java提供了Collections类的sort静态方法,可以直接对List进行排序,排序后的结果覆盖原来的List。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortList {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(1);
        list.add(5);
        list.add(2);
        Collections.sort(list);
        System.out.println(list);//[1, 2, 3, 5]
    }
}

sort方法默认按照自然排序进行排序,如果要自定义排序方式,可以实现Comparator接口并传入sort方法。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class SortListWithComparator {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("Tom", 20));
        list.add(new Student("Lucy", 19));
        list.add(new Student("Jan", 22));
        list.add(new Student("David", 18));
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println(list);//[Student{name='David', age=18}, Student{name='Lucy', age=19}, Student{name='Tom', age=20}, Student{name='Jan', age=22}]
    }
}

二、自然排序

如果List中的元素是Comparable接口的实现类,可以直接使用Collections.sort方法进行排序。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Person implements Comparable<Person> {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public int compareTo(Person o) {
        return this.age - o.age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class SortListWithComparable {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person("Tom", 20));
        list.add(new Person("Lucy", 19));
        list.add(new Person("Jan", 22));
        list.add(new Person("David", 18));
        Collections.sort(list);
        System.out.println(list);//[Person{name='David', age=18}, Person{name='Lucy', age=19}, Person{name='Tom', age=20}, Person{name='Jan', age=22}]
    }
}

三、流式排序

Java8中加入了Stream API,可以使用stream方法对List进行排序。

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

class Fruit {
    private String name;
    private int price;

    public Fruit(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Fruit{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

public class SortListWithStream {
    public static void main(String[] args) {
        List<Fruit> list = new ArrayList<>();
        list.add(new Fruit("apple", 5));
        list.add(new Fruit("banana", 3));
        list.add(new Fruit("watermelon", 10));
        list.add(new Fruit("pear", 4));
        List<Fruit> sortedList = list.stream()
                .sorted(Comparator.comparing(Fruit::getPrice))
                .toList();
        System.out.println(sortedList);//[Fruit{name='banana', price=3}, Fruit{name='pear', price=4}, Fruit{name='apple', price=5}, Fruit{name='watermelon', price=10}]
    }
}