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())); 

    }
}

配上张示例图片:

好啦,排序就完事儿了

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值