栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

代码实例

测试使用的实体类

package com.example.text_demo.test;

import lombok.Data;


@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;


public class test {

    public static void main(String[] args) {
        List 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() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println("升序 第二种方法"+studentList.toString());


        studentList.sort(new Comparator() {
            @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 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 ageOrder = Arrays.asList(11, 13, 9, 10);//定义排序使用的顺序集合

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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831327.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号