问题描述:

有一个list集合,其中元素是Student对象,根据student的age排序。

Student对象

/**
* description
*
* @author 70KG
* @date 2018/9/29
*/
@Data
public class Student implements Comparable<Student> { private String name; private Integer age; private Integer num; public Student() {
} public Student(String name, Integer age, Integer num) {
this.name = name;
this.age = age;
this.num = num;
} @Override
public int compareTo(Student student) {
return student.getAge().compareTo(this.getAge());
}
}

此类需要实现Comparable接口,重写compareTo方法

测试类:

/**
* description
*
* @author 70KG
* @date 2018/9/29
*/
public class TestController { public static void main(String[] args) { List<Student> list = new ArrayList<>(); Student student1 = new Student("张三",21,1);
Student student2 = new Student("李四",22,2);
Student student3 = new Student("王五",23,3);
Student student4 = new Student("赵六",24,4); list.add(student4);
list.add(student1);
list.add(student2);
list.add(student3); System.out.println(list); Collections.sort(list); System.out.println(list);
} }

利用Collections.sort()方法进行重排序。

输出结果:

[Student(name=赵六, age=24, num=4), Student(name=张三, age=21, num=1), Student(name=李四, age=22, num=2), Student(name=王五, age=23, num=3)]
[Student(name=赵六, age=24, num=4), Student(name=王五, age=23, num=3), Student(name=李四, age=22, num=2), Student(name=张三, age=21, num=1)]

正序倒序,只需改变实体中的compareTo方法即可。

关于List集合中元素排序问题的更多相关文章

  1. List集合中元素排序

    应用场景: 在开发中经常遇到要对List<Object>集合进行排序,并且是根据集合中的对象的某个属性来进行排序    --------以下就此做出的解决方案 public static ...

  2. C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响)

    C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响),如以下代码将无法通过编译. foreach (int x in myArray) { x++; //错误代码,因为改变 ...

  3. 对list集合中元素按照某个属性进行排序

    test 为集合中的元素类型(其中包含i属性) Collections.sort(list,(test o1, test o2) -> { if (o1.getI() != o2.getI()) ...

  4. Java删除List和Set集合中元素

    今天在做项目时,需要删除List和Set中的某些元素,当时使用边遍历,边删除的方法,却报了以下异常: ConcurrentModificationException 为了以后不忘记,使用烂笔头把它记录 ...

  5. MT【215】集合中元素个数

    设$M=\{1,2,3\cdots,2010\}$,$A$是$M$的子集且满足条件:当$x\in A$时$15x\notin A$,则$A$中的元素的个数最多是______ 分析:由于$x,15x,( ...

  6. java按照集合中元素的属性进行排序示例代码

    public class Student { private String name; private int age; private int id; public Student() {  sup ...

  7. More is better(hdu 1856 计算并查集集合中元素个数最多的集合)

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  8. c# Array或List有个很实用的ForEach方法,可以直接传入一个方法对集合中元素操作

    using System; using System.Collections.Generic; namespace demo { class Program { static void Main(st ...

  9. 用Scala实现集合中相邻元素间的差值

    欢迎转载,转载请注明出处,徽沪一郎. 概要 代码这东西,不写肯定不行,新学Scala不久,将实际遇到的一些问题记录下来,日后也好查找. 今天讲的是如何计算同一集合中元素两两之间的差值,即求开始集合(a ...

随机推荐

  1. Linux中DHCP服务器的简单配置(转)

    我安装了两台linux系统,一个作为服务器,一个客户端 两个都有3个网卡, 后两个网卡聚合为zhi一个网卡:Linux 网卡聚合 两台电脑都一样. 那么如何为这个聚合网卡进行DHCP的分配呢? 1.由 ...

  2. Java开发笔记(一百三十三)Swing的菜单

    传统的桌面程序基本是对某种类型的文件进行加工,例如Window自带的记事本用来读写文本文件,自带的画图程序用来查看和修改位图文件.为了方便用户切换各种操作,这些程序在窗口顶端放了一排菜单栏,单击菜单栏 ...

  3. C 风格字符串、string 类要点总结

    1. C风格字符串 1.1 其它 头文件<cstring> 特殊性质:C风格字符串以空字符\0结尾 1.2 读取一行的区别 1.2.1 cin.getline(array1,n,char) ...

  4. 用GDB调试程序(四)

    查看栈信息————— 当程序被停住了,你需要做的第一件事就是查看程序是在哪里停住的.当你的程序调用了一个函数,函数的地址,函数参数,函数内的局部变量都会被压入“栈”(Stack)中.你可以用GDB命令 ...

  5. 【微信小程序学习笔记】入门与了解

    [微信小程序学习笔记(一)] IDE 下载安装 下载地址 官方工具:https://mp.weixin.qq.com/debug/w … tml?t=1476434678461 下载可执行文件后,可按 ...

  6. Go语言(基本数据类型)

    Go语言中有丰富的数据类型,除了基本的整型.浮点型.布尔型.字符串外,还有数组.切片.结构体.函数.map.通道(channel)等.Go 语言的基本类型和其他语言大同小异. 基本数据类型 整型 整型 ...

  7. vue 仿写微信公众号自定义菜单

    先看效果图 代码参考 <template> <div> <!-- 公众号设置 --> <el-col :span="24" style=& ...

  8. 为什么我们要用Spring Boot?

    为什么我们要用 Spring Boot,Spring Boot 最重要的功能是:自动配置. 为什么说是自动配置? Spring Boot 的开启注解是:@SpringBootApplication,其 ...

  9. Python Django 协程报错,进程池、线程池与异步调用、回调机制

    一.问题描述 在Django视图函数中,导入 gevent 模块 import gevent from gevent import monkey; monkey.patch_all() from ge ...

  10. ConsoleLoggerExtensions.AddConsole(ILoggerFactory)已过时代码修复

    0x00.问题 netcoreapp2.2环境下, Startup.cs 代码配置如下 public void Configure(IApplicationBuilder app, IHostingE ...