ava集合的工具類Collections中提供了兩種排序的方法,分別是:
- Collections.sort(List list)
- Collections.sort(List list,Comparator c)
第一種稱為自然排序,參與排序的對象需實現comparable接口,重寫其compareTo()方法,方法體中實現對象的比較大小規則,示例如下:
實體類:(基本屬性,getter/setter方法,有參無參構造方法,toString方法)
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
|
package test; public class Emp implements Comparable { private String name; private int 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; } public Emp() { super (); } public Emp(String name, int age) { super (); this .name = name; this .age = age; } @Override public String toString() { return "Emp [name=" + name + ", age=" + age + "]" ; } @Override public int compareTo(Object o) { if (o instanceof Emp){ Emp emp = (Emp) o; // return this.age-emp.getAge();//按照年齡升序排序 return this .name.compareTo(emp.getName()); //換姓名升序排序 } throw new ClassCastException( "不能轉換為Emp類型的對象..." ); } } |
第二種叫定制排序,或自定義排序,需編寫匿名內部類,先new一個Comparator接口的比較器對象c,同時實現compare()其方法;
然后將比較器對象c傳給Collections.sort()方法的參數列表中,實現排序功能;
說明:第一種方法不夠靈活,實體類實現了comparable接口后,會增加耦合,如果在項目中不同的位置需要根據不同的屬性調用排序方法時,需要反復修改比較規則(按name還是按age),二者只能選擇其一,會起沖突.第二種就很好地解決了這個問題.在需要的地方,創建個內部類的實例,重寫其比較方法即可.
jUnit4單元測試類代碼如下:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; public class TestSort { static List list = new ArrayList(); //@BeforeClass注解標注的方法會在其它測試方法執行之前先執行, //且只執行一次.@Before注解標注的方法會在每個測試方法之前執行; //此處初始化集合只需要一次,因此使用@BeforeClass. @BeforeClass public static void init(){ list.add( new Emp( "tom" , 18 )); list.add( new Emp( "jack" , 20 )); list.add( new Emp( "rose" , 15 )); list.add( new Emp( "jerry" , 17 )); System.out.println( "排序前:" ); for (Object o : list){ System.out.println(o); } } /**按age升序排序*/ // @Test // public void testSortAge(){ // Collections.sort(list); // System.out.println("自然排序按age排序后:"); // for(Object o : list){ // System.out.println(o); // } // } // /**按name升序排序*/ @Test public void testSortName(){ Collections.sort(list); System.out.println( "自然排序按name升序排序后:" ); for (Object o : list){ System.out.println(o); } } /**使用Comparator比較器按age升序排序*/ @Test public void testComparatorSortAge(){ Collections.sort(list, new Comparator () { @Override public int compare(Object o1, Object o2) { if (o1 instanceof Emp && o2 instanceof Emp){ Emp e1 = (Emp) o1; Emp e2 = (Emp) o2; return e1.getAge() - e2.getAge(); } throw new ClassCastException( "不能轉換為Emp類型" ); } }); System.out.println( "使用Comparator比較器按age升序排序后:" ); for (Object o : list){ System.out.println(o); } } /**使用Comparator比較器按name升序排序*/ @Test public void testComparatorSortName(){ Collections.sort(list, new Comparator () { @Override public int compare(Object o1, Object o2) { if (o1 instanceof Emp && o2 instanceof Emp){ Emp e1 = (Emp) o1; Emp e2 = (Emp) o2; return e1.getName().compareTo(e2.getName()); } throw new ClassCastException( "不能轉換為Emp類型" ); } }); System.out.println( "使用Comparator比較器按name升序排序后:" ); for (Object o : list){ System.out.println(o); } } } |
右鍵空白位置 —> Run As —> JUnit Test —>
運行結果如下:
排序前:
Emp [name=tom, age=18]
Emp [name=jack, age=20]
Emp [name=rose, age=15]
Emp [name=jerry, age=17]
自然排序按name升序排序后:
Emp [name=jack, age=20]
Emp [name=jerry, age=17]
Emp [name=rose, age=15]
Emp [name=tom, age=18]
使用Comparator比較器按age升序排序后:
Emp [name=rose, age=15]
Emp [name=jerry, age=17]
Emp [name=tom, age=18]
Emp [name=jack, age=20]
使用Comparator比較器按name升序排序后:
Emp [name=jack, age=20]
Emp [name=jerry, age=17]
Emp [name=rose, age=15]
Emp [name=tom, age=18]
內容擴展:
使用重載的Collections.sort(List,Comparator)方法
方法一:自定義一個比較器然后傳入
Collections.sort(List,Comparator)中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
測試代碼: 自定義的比較器: import java.util.Comparator; public class MyComparator implements Comparator<Cell>{ @Override public int compare(Cell o1, Cell o2) { return o1.getY()-o2.getY(); //根據傳入的cell的y坐標由小到大進行排序 } } 測試類: @Test public void testComparator() { List<Cell> cells = new ArrayList<>(); cells.add( new Cell( 2 , 3 )); cells.add( new Cell( 5 , 1 )); cells.add( new Cell( 3 , 2 )); System.out.println(cells); //[Cell [x=2, y=3], Cell [x=5, y=1], Cell [x=3, y=2]] MyComparator com = new MyComparator(); Collections.sort(cells,com); System.out.println(cells); //根據自定義排序后的結果:[Cell [x=5, y=1], Cell [x=3, y=2], Cell [x=2, y=3]] } |
方法二:采用匿名內部的形式(推薦做法)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Test public void testComparator() { List<Cell> cells = new ArrayList<>(); cells.add( new Cell( 2 , 3 )); cells.add( new Cell( 5 , 1 )); cells.add( new Cell( 3 , 2 )); System.out.println(cells); //[Cell [x=2, y=3], Cell [x=5, y=1], Cell [x=3, y=2]] Collections.sort(cells, new Comparator<Cell>(){ //此處創建了一個匿名內部類 @Override public int compare(Cell o1,Cell o2){ return o1.getY() - o2.getY(); } }); System.out.println(cells); //[Cell [x=5, y=1], Cell [x=3, y=2], Cell [x=2, y=3]] } |
到此這篇關于java集合進行排序的方式總結的文章就介紹到這了,更多相關java集合進行排序的兩種方式內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:http://www.jszja.com/contents/14/2717.html