亲宝软件园·资讯

展开

Java ArrayList排序

IT利刃出鞘 人气:0

简介

说明

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

List排序方法

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

法1:JDK8的stream

见:一文详解Java中Stream流的使用

法2: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

法3: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

加载全部内容

相关教程
猜你喜欢
用户评论