方法一:實(shí)現(xiàn)comparator接口,并重寫(xiě)compare方法
實(shí)體類(lèi)代碼:
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; /** * 學(xué)生類(lèi) 方法一 * 實(shí)現(xiàn)comparator接口 * 并重寫(xiě)compare方法 * @author liaot * */ public class student implements comparator<student>{ private string name; //姓名 private int age; //年齡 //重寫(xiě) 比較方法 本次例子定義為按年齡比較 @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; } } |
測(cè)試類(lèi):
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) { //初始化四個(gè)不同的學(xué)生 student stu1 = new student( "路人甲" , 20 ); student stu2 = new student( "路人已" , 18 ); student stu3 = new student( "路人丙" , 16 ); student stu4 = new student( "路人丁" , 19 ); //新建list把學(xué)生加進(jìn)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); //第一個(gè)參數(shù)為list 第二個(gè)參數(shù)為對(duì)象的一個(gè)實(shí)例 system.out.println( "排序后:=====" ); for (student stu :stulist){ system.out.println( "姓名:" +stu.getname() + " 年齡" +stu.getage()); } } } |
運(yùn)行結(jié)果:
方法二:實(shí)現(xiàn)comparable接口 并重寫(xiě)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
|
/** * 學(xué)生類(lèi) 方法二 實(shí)現(xiàn)comparable接口 并重寫(xiě)compareto方法 * * @author liaot * */ public class student2 implements comparable<student2> { private string name; // 姓名 private int age; // 年齡 // 重寫(xiě) 比較方法 本次例子定義為按年齡比較 @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; } } |
測(cè)試類(lèi)
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) { //初始化四個(gè)不同的學(xué)生 student2 stu1 = new student2( "路人甲" , 20 ); student2 stu2 = new student2( "路人已" , 18 ); student2 stu3 = new student2( "路人丙" , 16 ); student2 stu4 = new student2( "路人丁" , 19 ); //新建list把學(xué)生加進(jìn)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); //只有一個(gè)參數(shù)參數(shù)為list system.out.println( "排序后:=====" ); for (student2 stu :stulist){ system.out.println( "姓名:" +stu.getname() + " 年齡" +stu.getage()); } } } |
運(yùn)行結(jié)果
三、總結(jié):兩種方式寫(xiě)法和用法上的區(qū)別:
以上這篇java根據(jù)list內(nèi)對(duì)象的屬性排序方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/c1481118216/article/details/53496083