List集合按字段排序并输出

先建立一个实体:

这个实体就是简单的学生表实体(共有id,name,age,createDatetime四个字段)

package com.test.sort;

/**
 * @author wang
 * @date 2018/11/29
 */
public class StudentEntity implements Comparable<StudentEntity> {

    private int id;
    private String name;
    private int age;
    private String createDatetime;

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public String getCreateDatetime() {
        return createDatetime;
    }

    public void setCreateDatetime(String createDatetime) {
        this.createDatetime = createDatetime;
    }

    @Override
    public int compareTo(StudentEntity o) {
        return name.compareTo(o.getName());
    }


}

在main方法我们来实现各种情况的排序:

package com.test.sort;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wang
 * @date 2018/11/29
 */
public class Stream {

    public static void main(String[] args) {

        //插入三条数据
        List<StudentEntity> studentList = Arrays.asList(
                new StudentEntity(1, "name1", 10, "2018-11-29"),
                new StudentEntity(2, "name2", 8, "2018-11-30"),
                new StudentEntity(3, "name3", 28, "2018-11-19")
        );

        //根据第一个字段id正序
        List<StudentEntity> studentList1 = studentList.stream().sorted().collect(Collectors.toList()); 

        //根据第一个字段id逆序
        List<StudentEntity> studentList2 = studentList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()); 

        //根据年龄正序(int类型)
        List<StudentEntity> studentList3 = studentList.stream().sorted(Comparator.comparingInt(StudentEntity::getAge)).collect(Collectors.toList()); 

        //根据年龄逆序(int类型)
        List<StudentEntity> studentList4 = studentList.stream().sorted(Comparator.comparingInt(StudentEntity::getAge).reversed()).collect(Collectors.toList()); 

        //根据时间正序(String类型)
        List<StudentEntity> studentList5 = studentList.stream().sorted(Comparator.comparing(StudentEntity::getCreateDatetime)).collect(Collectors.toList()); 

        //根据时间逆序(String类型)
        List<StudentEntity> studentList6 = studentList.stream().sorted(Comparator.comparing(StudentEntity::getCreateDatetime).reversed()).collect(Collectors.toList()); 

        //打印结果
        studentList6.forEach(student -> System.out.println("id is " + student.getId() + " ;  name is " + student.getName() + ";  age is " + student.getAge() + ";  time is " + student.getCreateDatetime())); 

    }
}

配上张示例图片:

好啦,排序就完事儿了

发布了11 篇原创文章 · 获赞 5 · 访问量 7948

猜你喜欢

转载自blog.csdn.net/Super_King_/article/details/84617904