1. 程式人生 > >List集合根據儲存物件的屬性欄位排序實現

List集合根據儲存物件的屬性欄位排序實現

轉載:來自:https://blog.csdn.net/u013821825/article/details/61202287 

 java8之前的用法:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
 * ListSort類
 *
 * @author Lance
 * @date 2017/03/10
 */
public class ListSort {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();
        students.add(new Student("張三", 24));
        students.add(new Student("李四", 23));
        students.add(new Student("王五", 25));
        System.out.println("before sort:");
        for (Student bean : students) {
            System.out.println("before sort---->age: " + bean.getAge());
        }
        Collections.sort(students);
        System.out.println("after sort:");
        for (Student bean : students) {
            System.out.println("after sort---->age: " + bean.getAge());
        }
    }
}
class Student implements Comparable<Student> {
    private String name;
    private Integer age;
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    /*
     * s的age小於this的對應欄位,則返回負數;
     * 大於則返回正數;
     * 建議去了解compareTo背後的真相
     */
    @Override
    public int compareTo(Student s) {
        return s.getAge().compareTo(this.getAge());
    }

    public Integer getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  1. 預期結果
Connected to the target VM, address: '127.0.0.1:3850', transport: 'socket'
before sort:
before sort---->age: 24
before sort---->age: 23
before sort---->age: 25
after sort:
after sort---->age: 25
after sort---->age: 24
after sort---->age: 23

======================================================

 
以上是java8之前的用法,java8提供了多種操作方式。博文修改2017.4.7

  • 實體類

@AllArgsConstructor
@NoArgsConstructor
@Data
class Teacher {
    private String name;
    private Integer age;
}
  • 使用Lambda表示式
 List<Teacher> teachers = new ArrayList<Teacher>();
        teachers.add(new Teacher("張三", 24));
        teachers.add(new Teacher("李四", 23));
        teachers.add(new Teacher("王五", 25));
        System.out.println("before sort:");
        for (Teacher bean : teachers) {
            System.out.println("before sort---->age: " + bean.getAge());
        }
        //Lambda
        teachers.sort((s1, s2) -> s1.getAge().compareTo(s2.getAge()));
        System.out.println("after sort:");
        for (Teacher bean : teachers) {
            System.out.println("after sort---->age: " + bean.getAge());
        }
  • 使用Comparator操作器
//Comparator
        Collections.sort(teachers, Comparator.comparing(Teacher::getAge));
  • 反序排列
Comparator<Teacher> comparator = (t1, t2) -> t1.getAge().compareTo(t2.getAge());
        teachers.sort(comparator.reversed());
  • 組合排序
//組合排序
        teachers.sort(Comparator.comparing(Teacher::getAge).thenComparing(Teacher::getName));
  • 完整示例程式碼
package com.huixin.utils.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * ListSort類
 *
 * @author Lance
 * @date 2017/03/10
 */
public class ListSortJava8 {
    public static void main(String[] args) {
        List<Teacher> teachers = new ArrayList<Teacher>();
        teachers.add(new Teacher("張三", 24));
        teachers.add(new Teacher("李四", 23));
        teachers.add(new Teacher("王五", 25));
        teachers.add(new Teacher("錢二", 23));
        System.out.println("before sort:");
        for (Teacher bean : teachers) {
            System.out.println("before sort---->age: " + bean.getAge() + "==name:" + bean.getName());
        }
        //Lambda
//        teachers.sort((t1, t2) -> s1.getAge().compareTo(s2.getAge()));
        //Comparator
//        Collections.sort(teachers, Comparator.comparing(Teacher::getAge));
        //reversed
//        Comparator<Teacher> comparator = (t1, t2) -> t1.getAge().compareTo(t2.getAge());
//        teachers.sort(comparator.reversed());
        //組合排序
        teachers.sort(Comparator.comparing(Teacher::getAge).thenComparing(Teacher::getName));
        System.out.println("after sort:");
        for (Teacher bean : teachers) {
            System.out.println("after sort---->age: " + bean.getAge() + "==name:" + bean.getName());
        }
    }
}

@AllArgsConstructor
@NoArgsConstructor
@Data
class Teacher {
    private String name;
    private Integer age;
}