java根据List内对象的属性排序

原创 2016年12月07日 00:20:01
  • 2625

方法一:实现Comparator接口,并重写compare方法

  • 实体类代码:
import java.util.Comparator;

/**
* 学生类 方法一
* 实现Comparator接口
* 并重写compare方法
* @author liaot
*
*/
public class Student implements Comparator<Student>{
private String name; //姓名
private int age; //年龄 //重写 比较方法 本次例子定义为按年龄比较
@Override
public int compare(Student o1, Student o2) {
if(o1.getAge() > o2.getAge()){
return 1;
}else{
return -1;
}
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} 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;
}
}
  • 测试类:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class Main { public static void main(String[] args) {
//初始化四个不同的学生
Student stu1 = new Student("路人甲", 20);
Student stu2 = new Student("路人已", 18);
Student stu3 = new Student("路人丙", 16);
Student stu4 = new Student("路人丁", 19);
//新建List把学生加进List
List<Student> stuList = new ArrayList<>();
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
System.out.println("排序前:=====");
for(Student stu :stuList){
System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
}
//排序
Collections.sort(stuList, stu1); //第一个参数为List 第二个参数为对象的一个实例
System.out.println("排序后:=====");
for(Student stu :stuList){
System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
}
} }
  • 运行结果: 

方法二:实现Comparable接口 并重写compareTo方法

/**
* 学生类 方法二 实现Comparable接口 并重写compareTo方法
*
* @author liaot
*
*/
public class Student2 implements Comparable<Student2> {
private String name; // 姓名
private int age; // 年龄 // 重写 比较方法 本次例子定义为按年龄比较
@Override
public int compareTo(Student2 stu) {
if (this.age > stu.getAge()) {
return 1;
} else {
return -1;
}
} public Student2(String name, int age) {
super();
this.name = name;
this.age = age;
} 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;
} }
  • 测试类
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class Main2 { public static void main(String[] args) {
//初始化四个不同的学生
Student2 stu1 = new Student2("路人甲", 20);
Student2 stu2 = new Student2("路人已", 18);
Student2 stu3 = new Student2("路人丙", 16);
Student2 stu4 = new Student2("路人丁", 19);
//新建List把学生加进List
List<Student2> stuList = new ArrayList<>();
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
System.out.println("排序前:=====");
for(Student2 stu :stuList){
System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
}
//排序
Collections.sort(stuList); //只有一个参数参数为List
System.out.println("排序后:=====");
for(Student2 stu :stuList){
System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
}
} }
  • 运行结果 

三、总结:两种方式写法和用法上的区别:

版权声明:欢迎转载,请注明原文地址:http://blog.csdn.net/c1481118216 http://blog.csdn.net/c1481118216/article/details/53496083,http://blog.csdn.net/c1481118216/article/details/53496083
 
 

list集合排序2的更多相关文章

  1. Java比较器对数组,集合排序一

    数组排序非常简单,有前辈们的各种排序算法,再加上Java中强大的数组辅助类Arrays与集合辅助类Collections,使得排序变得非常简单,如果说结合比较器Comparator接口和Collato ...

  2. ArrayList集合排序

    using System;using System.Collections;using System.Collections.Generic;using System.Text; namespace ...

  3. 【Java进阶】---map集合排序

    map集合排序         这篇文章讲的不仅仅是map排序,比如把对象按某一属性排序,它都可以解决这些问题.   比如,有N个对象,每个对象有个属性就是成绩,成绩分:优秀,良好,合格.那我们如何按 ...

  4. CopyOnWriteArrayList集合排序异常问题

    1.集合自定义排序实现 对List集合的自定义排序想必大家都知道要使用如下的方式,通过实现Comparator接口并实现compare方法来实现. /** * * @方法名 changeChain * ...

  5. 二维码扫描&集合排序

    一.二维码扫描机制 二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的:在代码编制上巧妙地利用构 ...

  6. .Net中集合排序的一种高级玩法

    背景: 学生有名称.学号, 班级有班级名称.班级序号 学校有学校名称.学校编号(序号) 需求 现在需要对学生进行排序 第一排序逻辑 按学校编号(序号)排列 再按班级序号排列 再按学生学号排列 当然,在 ...

  7. Java集合排序及java集合类详解--(Collection, List, Set, Map)

    1         集合框架 1.1         集合框架概述 1.1.1         容器简介 到目前为止,我们已经学习了如何创建多个不同的对象,定义了这些对象以后,我们就可以利用它们来做一 ...

  8. Java提高(5)---map集合排序

    map集合排序 这篇文章讲的不仅仅是map排序,比如把对象按某一属性排序,它都可以解决这些问题. 比如,有N个对象,每个对象有个属性就是成绩,成绩分:优秀,良好,合格.那我们如何按照成绩的好坏进行排序 ...

  9. 集合排序 Comparator和Comparable的使用区别

    Java 排序 Compare  Comparator接口 Comparable接口 区别 在Java中使用集合来存储数据时非常常见的,集合排序功能也是常用功能之一.下面看一下如何进行集合排序,常用的 ...

  10. map集合排序

    默认情况下,HashMap.HashTable.TreeMap.LinkedHashMap的排列顺序比较: package com.per.sdg.demo; import java.util.Has ...

随机推荐

  1. IDEA上传项目到SVN

    1.打开IDEA ,上面工具栏选择VCS 选择把项目交给SVN管理 2.选择SVN 3.选择SVN管理后可以看到项目变这个颜色 4.右键项目选择如下 5.点击绿色的+号,选择一个SVN仓库的地址,下面 ...

  2. 并发编程(六)——进程/线程池、协程、gevent第三方库

    进程/线程池.协程.gevent第三方库 一.进程/线程池 1.进程池 (1)什么是进程池 如果需要创建的子进程数量不大,可以直接利用multiprocess中的Process来创建.但是当需要创建上 ...

  3. li中下的a元素的字超出了li的宽度

    网站搬迁,给你带来的不便敬请谅解! http://www.suanliutudousi.com/2017/10/21/li%E4%B8%AD%E4%B8%8B%E7%9A%84a%E5%85%83%E ...

  4. 人工智能都能写Java了!这款插件让你编程更轻松

    最近在浏览技术社区,发现了一款 IDE 插件,利用人工智能技术帮助程序员高效写代码.节省开发时间,一下子勾起了我的好奇心. 下载之后,使用一番,确实蛮好的,可以有效提升编程效率. 这款插件叫:aixc ...

  5. 第四记 Java异常

    Java异常结构图 Java所有异常都是从Throwable继承而来,Throwable有两个子类,Error与Exception. Error是错误,对于所有的编译时期的错误以及系统错误都是通过Er ...

  6. 防止DDOS攻击有效方法:隐藏服务器真实IP

    如今,网站服务器的安全受到越来越多的重视,但是难免会遇到黑客使用DDoS攻击网站,为了网站的安全通常都会做好防御,其中防止DDoS攻击有效方法:隐藏服务器真实IP ,该技术能够有效地保护网站的安全. ...

  7. pytest_fixture--scope="session"

    import pytest@pytest.fixture(scope="session")def login(): print("\n输入用户名密码登陆! configt ...

  8. Flyway 学习时遇到的错误

    错误一: No plugin found for prefix 'flyway' in the current project and in the plugin groups  找不到Flyway插 ...

  9. css translate/rotate 空间坐标轴

    参考:https://www.cnblogs.com/zhangnan35/p/10709876.html https://www.cnblogs.com/zyrblog/p/11142624.htm ...

  10. spring_入门配置和注入

    Spring的获取容器: public static void main(String[] args) { //获取核心容器 BeanFactory延迟加载对象 ApplicationContext ...