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

    }
}

配上張示例圖片:

好啦,排序就完事兒了

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章