所有分类
  • 所有分类
  • 未分类

Java之List-排序的方法

简介

说明

本文用示例介绍Java的ArrayList排序的方法。

List排序方法

主要有三种方法(按推荐度排序):

  1. JDK8的stream
  2. Comparator#compare()
  3. Comparable#compareTo()

法1:list的sort()

代码

package com.example.a;

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


class User{

    private Integer score;

    private Integer age;

    public User(Integer score, Integer age){
        super();
        this.score = score;
        this.age = age;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}


public class Demo {
    public static void main(String[] args) {
        List<User> users = new ArrayList<>();
        users.add(new User(95, 26));
        users.add(new User(84, 23));
        users.add(new User(96, 25));
        users.add(new User(95, 24));

        // 单字段排序
        users.sort(Comparator.comparing(User::getAge));
        for(User user : users){
            System.out.println(user.getScore() + "," + user.getAge());
        }

        System.out.println("---------------------------------");

        // 多字段排序(法1)
        users.sort((o1, o2) -> {
            // 这里必须要在中间加分隔符。否则,若都是数字,会变成数字相加,再转为字符串
            String tmp1 = o1.getScore() + ":" + o1.getAge();
            String tmp2 = o2.getScore() + ":" + o2.getAge();
            return tmp1.compareTo(tmp2);
        });
        for(User user : users){
            System.out.println(user.getScore() + ":" + user.getAge());
        }

        System.out.println("---------------------------------");

        // 多字段排序(法2)
        users.sort((o1, o2) -> {
            int i = o2.getScore() - o1.getScore();
            if (i == 0) {
                return o1.getAge() - o2.getAge();
            }
            return i;
        });
        for(User user : users){
            System.out.println(user.getScore() + "," + user.getAge());
        }
    }

}

测试

84,23
95,24
96,25
95,26
---------------------------------
84:23
95:24
95:26
96:25
---------------------------------
96,25
95,24
95,26
84,23

法2:JDK8的stream

见:Java–Stream(流)–使用/实例/流操作 – 自学精灵

法3:Comparator#compare()

需求:用户有成绩和年龄。按成绩排序,若成绩相同,则按年龄排序。

package org.example.a;

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


class User{

    private int score;

    private int age;

    public User(int score, int age){
        super();
        this.score = score;
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}


public class Demo {
    public static void main(String[] args) {
        List<User> users = new ArrayList<User>();
        users.add(new User(95, 26));
        users.add(new User(84, 23));
        users.add(new User(96, 25));
        users.add(new User(95, 24));
        Collections.sort(users, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                int i = o2.getScore() - o1.getScore();
                if(i == 0){
                    return o1.getAge() - o2.getAge();
                }
                return i;
            }
        });
        for(User user : users){
            System.out.println(user.getScore() + "," + user.getAge());
        }
    }

}

执行结果

96,25
95,24
95,26
84,23

法4:Comparable#compareTo()

默认按增序排序:

package org.example.a;

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

public class Demo {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(3);
        nums.add(5);
        nums.add(1);
        nums.add(0);
        System.out.println(nums);
        Collections.sort(nums);
        System.out.println(nums);
    }

}

执行结果:

[3, 5, 1, 0]
[0, 1, 3, 5]

需求:用户有成绩和年龄。按成绩降序排序,若成绩相同,则按年龄正序排序。

package org.example.a;

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


class User implements Comparable<User>{

    private int score;

    private int age;

    public User(int score, int age){
        super();
        this.score = score;
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(User o) {
        int i = o.getScore() - this.getScore();
        if(i == 0){
            return this.getAge() - o.getAge();
        }
        return i;
    }
}


public class Demo {
    public static void main(String[] args) {
        List<User> users = new ArrayList<User>();
        users.add(new User(95, 26));
        users.add(new User(84, 23));
        users.add(new User(96, 25));
        users.add(new User(95, 24));
        Collections.sort(users);
        for(User user : users){
            System.out.println(user.getScore() + "," + user.getAge());
        }
    }

}

执行结果

96,25
95,24
95,26
84,23
2

评论0

请先

显示验证码
没有账号?注册  忘记密码?

社交账号快速登录