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接口 首先我们要知道,传递对象,需要先将对象序列化 一.那么为什么要 ...
随机推荐
- 主机(windows)与VMware虚拟机(linux)互传文件
网上有不少教程,比如http://bbs.kafan.cn/thread-451327-1-1.html,我仅记录我平时用的方法. 1.VMware tools: 如果有网,且Linux有桌面的,可 ...
- 算法Sedgewick第四版-第1章基础-019一Scanner的用法
package algorithms.fundamentals001; import java.util.Locale; import java.util.Scanner; import algori ...
- Luogu 3911 最小公倍数之和
感觉自己被早上的名校协作体和下午的数学题虐哭了,每天为自己的菜发愁…… 发现$a_{i}$很小,开一个桶记一下每个数 出现的个数,设$c_{i} = \sum_{j = 1}^{n}(a_{j} == ...
- 20169206 2016-2017-2 《网络攻防实践》 nmap的使用
Part I 使用nmap扫描ubuntu靶机 先给出nmap的官方中文操作手册https://nmap.org/man/zh/,其实并不太好用,而且有时候会打不开,但至少是官方手册. 探查操作系统 ...
- [译]Javascript中的函数
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- MacBook Pro (13 英寸, 2017 年)安装win10系统
准备: windows10系统镜像 16G或更大容量U盘(存放驱动,必须格式化为FAT) 官方提供的驱动软件 详细步骤: 1.打开Bootcamp,选择镜像文件 2.点击下一步,选择Bootcamp分 ...
- Head First HTML与CSS(第2版) 中文pdf扫描版
是不是已经厌倦了那些深奥的HTML书?你可能在抱怨,只有成为专家之后才能读懂那些书.那么,找一本新修订的<Head First HTML与CSS(第2版)>吧,来真正学习HTML.你可能希 ...
- python语言积累
调试打印堆栈 import traceback traceback.print_exc() #打印堆栈的详细信息
- cross validation
k-folder cross-validation:k个子集,每个子集均做一次测试集,其余的作为训练集.交叉验证重复k次,每次选择一个子集作为测试集,并将k次的平均交叉验证识别正确率作为结果.优点:所 ...
- ubuntu - 14.04,安装rpm程序!!
一,安装rpm转deb的工具“alien”:在软件中心里面输入“alien”,看是否已经安装,如果没有安装则直接安装. 二,把rpm转换为deb:在shell里输入“sudo alien --scri ...