JAVA集合对指定字段进行排序操作

业务中数据一般都是放在一个集合中如:Map ,Set ,List 等集合中。他们都提供了一个排序方法 sort(),要对数据排序直接使用这个方法就行,但是要保证集合中的对象是 可比较的。

代码实例

测试使用的实体类

package com.example.text_demo.test;

import lombok.Data;

/**
 * @author L_ds
 * @date 2022/4/22 10:44
 */
@Data
public class Student {
    int id;
    int age;
    String name;

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

测试类

单个字段的排序

package com.example.text_demo.test;

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

import static java.util.Comparator.comparing;

/**
 * @author L_ds
 * @date 2022/4/22 10:52
 */
public class test {

    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<>();
        Student student = new Student(1,11,"张三");
        Student student1 = new Student(2,10,"李四");
        Student student2 = new Student(2,9,"王五");
        Student student3 = new Student(2,13,"赵六");
        studentList.add(student);
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        System.out.println("初始"+studentList.toString());
        //升序 第一种方法
        studentList.sort(comparing(Student::getAge));

        System.out.println("升序 第一种方法"+studentList.toString());
        //降序 第一种方法
        studentList.sort(comparing(Student::getAge).reversed());

        System.out.println("降序 第一种方法"+studentList.toString());

        studentList.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println("升序 第二种方法"+studentList.toString());


        studentList.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        System.out.println("降序 第二种方法"+studentList.toString());
    }

}


运行效果

初始[Student(id=1, age=11, name=张三), Student(id=2, age=10, name=李四), Student(id=2, age=9, name=王五), Student(id=2, age=13, name=赵六)]
升序 第一种方法[Student(id=2, age=9, name=王五), Student(id=2, age=10, name=李四), Student(id=1, age=11, name=张三), Student(id=2, age=13, name=赵六)]
降序 第一种方法[Student(id=2, age=13, name=赵六), Student(id=1, age=11, name=张三), Student(id=2, age=10, name=李四), Student(id=2, age=9, name=王五)]
升序 第二种方法[Student(id=2, age=9, name=王五), Student(id=2, age=10, name=李四), Student(id=1, age=11, name=张三), Student(id=2, age=13, name=赵六)]
降序 第二种方法[Student(id=2, age=13, name=赵六), Student(id=1, age=11, name=张三), Student(id=2, age=10, name=李四), Student(id=2, age=9, name=王五)]

描述:排序如果使用Collections.sort(集合);的话则需要在对应的实体类实现Comparable类,然后重写Comparable方法。
此处我第一种方法,我是使用集合的sort方法,然后使用comparing(),再填入需要排序的字段,比重写方法的实现更灵活。使用sort为升序,如果想降序,则使用reversed()方法;.
第二种方法是在sort中实现Comparator的方法,然后在return处get需要进行排序的字段 ,此处需要着重说 public int compare(Student o1, Student o2) {return o2.getAge() - o1.getAge();}这个方法,return处比较运算这块,会返回三种int类型的数值:负整数,零,正整数。
return o2.getAge() - o1.getAge()为负整数时,o2位置排在前面,当为0时,位置不变,当为正整数时,o2位置排在后面,

进阶

以上的排序都是按照依次顺序进行排序(如:1、2、3;或者 3、2、1),那想实现年龄根据自定义规则如何实现(如:3、1、2;或者 1、3、2)。

    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<>();
        Student student = new Student(1,11,"张三");
        Student student1 = new Student(2,10,"李四");
        Student student2 = new Student(2,9,"王五");
        Student student3 = new Student(2,13,"赵六");
        studentList.add(student);
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        System.out.println("初始"+studentList.toString());

        List<Integer> ageOrder = Arrays.asList(11, 13, 9, 10);//定义排序使用的顺序集合

        Collections.sort(studentList, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
               int io1 = ageOrder.indexOf(s1.getAge());
               int io2 = ageOrder.indexOf(s2.getAge());
                return io1 - io2;
              /* return io2 - io1;*/
            }
        });

        System.out.println("排序后"+studentList.toString());
    }

尾结

方法要学会举一反三,不能硬套,以上都是单字段排序,还有多字段排序等等,学以致用

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值