List集合根据存储对象的属性字段排序实现
Posted Lance_xu_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了List集合根据存储对象的属性字段排序实现相关的知识,希望对你有一定的参考价值。
- List集合根据存储对象的属性字段排序实现
package com.huixin;
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;
- 预期结果
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;
以上是关于List集合根据存储对象的属性字段排序实现的主要内容,如果未能解决你的问题,请参考以下文章
多个相同的对象存储在List集合,根据对象内的某一个属性去重并且实现其中一个字段相加
多个相同的对象存储在List集合,根据对象内的某一个属性去重并且实现其中一个字段相加