Java对象排序两种方法
转载:https://blog.csdn.net/wangtaocsdn/article/details/71500500
有时候需要对对象列表或数组进行排序,下面提供两种简单方式:
方法一:将要排序的对象类实现Comparable<>接口。
首先,创建学生类,我们将根据学生成绩对学生进行排序:
/**
* 学生类
*/
class Student implements Comparable<Student>{
String name;
int age;
int score;
public Student(String name, int age,int score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Studento) {
// TODO Auto-generated method stub
return this.age - o.age;
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("大铭", 19, 89));
students.add(new Student("来福", 26, 90));
students.add(new Student("仓颉", 23, 70));
students.add(new Student("王磊", 18, 80));
System.out.println("排序前:");
for (Student student : students) {
System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
}
// 排序
Collections.sort(students);
System.out.println("排序后:");
for (Student student : students) {
System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
}
}
}
同理,也可以根据对象的其他属性进行排序。
方法二:使用Comparator匿名内部类实现。
还是使用同一个例子,按成绩将学生排序:
/**
* 学生类
*/
class Student {
String name;
int age;
int score;
public Student(String name, int age,int score) {
this.name = name;
this.age = age;
this.score = score;
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("大铭", 19, 89));
students.add(new Student("来福", 26, 90));
students.add(new Student("仓颉", 23, 70));
students.add(new Student("王磊", 18, 80));
System.out.println("排序前:");
for (Student student : students) {
System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
}
Collections.sort(students,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.age-o2.age;
}
});
System.out.println("排序后:");
for (Student student : students) {
System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
}
}
}
也可以实现按对象属性将对象列表排序。
Java对象排序两种方法的更多相关文章
- 读取xml文件转成List<T>对象的两种方法(附源码)
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...
- 取xml文件转成List<T>对象的两种方法
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...
- Intent传递对象的两种方法(Serializable,Parcelable) (转)
今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...
- [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)
http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...
- WCF生成客户端代理对象的两种方法的解释
最近在封装WCF,有一些很好的实践就记录下来,大家可以放心使用,所有代码都已经调试过.如果有高手可以大家探讨一下. 在WCF中有两种不同的方法可以用于创建客户端服务对象,他们分别为: 1. 代理构造法 ...
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
[转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...
- 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)
Delphi是市场上最好的RAD工具,但是现在C++占据着主导地位,有时针对一个问题很难找到Delphi或Pascal的解决方案.可是却可能找到了一个相关的C++类.本文描述几种在Delphi代码中使 ...
- Intent传递对象的两种方法
Android为intent提供了两种传递对象参数类型的方法 分别需要使实体类实现Serializable接口.Parcelable接口 首先我们要知道,传递对象,需要先将对象序列化 一.那么为什么要 ...
随机推荐
- AWT简介
-------------siwuxie095 AWT 简介: AWT(Abstract Window Toolkit)是最原始的 Java G ...
- php学习笔记-include
这个和C语言中的include是同样的用法,如果一个php文件中有一句include a.php,那么执行的时候就会把a.php的内容插入到这个php文件中. 举个例子,我现在有一个名为hellowo ...
- Luogu 3193 [HNOI2008]GT考试
BZOJ1009 妙! 推荐这篇题解: https://www.luogu.org/blog/Edgration/solution-p3193 考虑设计dp,设$f_{i, j}$表示长串匹配到i,短 ...
- ubuntu 12.04 (64位)下安装oracle 11g过程及问题总结
最近公司用到oracle,在ubuntu64位安装了一下,碰到了一些问题,在网上搜索到了一些答案,在此作为笔记记录下来. 1.首先下载oracle并解压不再赘述. 2.安装依赖包 sudo apt-g ...
- windows脚本设置网络IP地址
需求描述 不通的网络环境下,可能需要设置静态IP地址,或设置为动态获取,每次重复手动的配置费时费力,通过脚本可以实现一键设置 脚本实现 1.设置静态IP 1.1新建文本文档,复制粘贴如下内容 nets ...
- C/C++使用心得:enum与int的相互转换
如何正确理解enum类型? 例如: enum Color { red, white, blue}; Color x; 我们应说x是Color类型的,而不应将x理解成enumeration类型,更不应将 ...
- java的一些最最最最基本的东西,纯粹是为了保存
1.方法签名 指的是方法名和参数类型 2.java类初始化数据的方法 构造函数 声明变量时赋值 静态块 3.List转数组 List<String> list = new ArrayLis ...
- ubuntu - 安装hive
粗略步骤: 详细参考:https://www.2cto.com/net/201804/735478.html 环境:ubunut jdk hadoop mysql 一.下载hive 二.解压( ...
- [python]glob模块中的glob()函数为什么返回空列表??
最近在学习语音的知识,看一个语音合成实现的相关工具包的源代码,碰到了glob()函数.然后开启了我与这个函数相爱想杀的一个下午. 摘自官网解释: https://docs.python.org/2/l ...
- NSNull空值
1.前言 作为占据空间的一个空值,如用在数组或字典中占据一个没有任何值的空间. 1.1 NULL & nil 的区别: nil 是 OC 的,空对象,地址指向空的对象,指针地址指向的是 NUL ...