方法一:实现comparator接口,并重写compare方法
实体类代码:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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;
}
}
|
测试类:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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方法
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
/**
* 学生类 方法二 实现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;
}
}
|
测试类
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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());
}
}
}
|
运行结果
三、总结:两种方式写法和用法上的区别:
以上这篇java根据list内对象的属性排序方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/c1481118216/article/details/53496083
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
系统监控中心:一个理想的 Linux 任务管理器和资源监视器
2025-05-25 31 -
2025-06-04 54
-
2025-05-27 46
-
2025-05-29 86
-
2025-06-04 37
热门评论




