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

2025-05-27 0 89

方法一:实现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());

}

}

}

运行结果:

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

方法二:实现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内对象的属性排序方法

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

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

以上这篇java根据list内对象属性排序方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/c1481118216/article/details/53496083

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java根据List内对象的属性排序方法 https://www.kuaiidc.com/76640.html

相关文章

发表评论
暂无评论