1. sort
對集合進行排序
1 public static <T extends Comparable<? super T>> void sort(List<T> list) 2 3 public static <T> void sort(List<T> list, 4 Comparator<? super T> c)
在使用List時想根據List中存儲對象的某一字段進行排序,那么我們要用到Collections.sort方法對list排序,用Collections.sort方法對list排序有兩種方法:
- 第一種是list中的對象實現Comparable接口;
- 第二種方法是根據Collections.sort重載方法來實現。
示例如下:
1 public class SortTest {
2 public static void main(String[] args) {
3 List<String> listS = new ArrayList<String>();
4 List<Employer1> list1 = new ArrayList<Employer1>();
5 List<Employer2> list2 = new ArrayList<Employer2>();
6 List<Employer3> list3 = new ArrayList<Employer3>();
7
8 //一.將String類型的變量插入到listS中並排序
9 //listS中的對象String 本身含有compareTo方法,所以可以直接調用sort方法,按自然順序排序,即升序排序
10 listS.add("5");
11 listS.add("2");
12 listS.add("9");
13 Collections.sort(listS);
14
15 //二.將Employer1類的對象插入到list1中並排序
16 //將已創建的實現了Comparator接口的比較類MyCompare傳入Collections的sort方法中即可實現依照MyCompare類中的比較規則。
17 Employer1 a1 = new Employer1();
18 Employer1 b1 = new Employer1();
19 Employer1 c1 = new Employer1();
20 a1.setName("a1"); a1.setAge(44);
21 b1.setName("b1"); b1.setAge(55);
22 c1.setName("b1"); c1.setAge(33);
23 list1.add(a1);
24 list1.add(b1);
25 list1.add(c1);//Collections類的sort方法要求傳入的第二個參數是一個已實現Comparator接口的比較器
26 Collections.sort(list1, new MyCompare());
27
28 //三.將Employer2類的對象插入到list2中並排序
29 //其實原理和上面的二類似,只是沒有單獨創建MyCompare類,而是用匿名內部類來實現Comparator接口里面的具體比較。
30 Employer2 a2 = new Employer2();
31 Employer2 b2 = new Employer2();
32 Employer2 c2 = new Employer2();
33 a2.setName("a2"); a2.setAge(66);
34 b2.setName("b2"); b2.setAge(33);
35 c2.setName("b2"); c2.setAge(22);
36 list2.add(a2);
37 list2.add(b2);
38 list2.add(c2); //Collections類的sort方法要求傳入的第二個參數是一個已實現Comparator接口的比較器
39 Collections.sort(list2,new Comparator<Employer2>(){
40 @Override
41 public int compare(Employer2 a2, Employer2 b2) {
42 return a2.getOrder().compareTo(b2.getOrder());
43 }
44
45 });
46
47 //四.將Employer3類的對象插入到list3中並排序
48 //被排序的類Employer3實現了Comparable接口,在類Employer3中通過重載compareTo方法來實現具體的比較。
49 Employer3 a3 = new Employer3();
50 Employer3 b3 = new Employer3();
51 Employer3 c3 = new Employer3();
52 a3.setName("a3"); a3.setAge(77);
53 b3.setName("b3"); b3.setAge(55);
54 c3.setName("b3"); c3.setAge(99);
55 list3.add(a3);
56 list3.add(b3);
57 list3.add(c3);
58 Collections.sort(list3);//Collections類的sort方法要求傳入的List中的對象是已實現Comparable接口的對象
59
60 System.out.println(listS);
61 System.out.println(list1);
62 System.out.println(list3);
63 System.out.println(list2);
64 }
65 }
66 class Employer1{
67 private String name;
68 private Integer age;
69 public void setName(String name) {
70 this.name = name;
71 }
72 public Integer getAge() {
73 return age;
74 }
75 public void setAge(Integer age) {
76 this.age = age;
77 }
78 @Override//重載了Object類里的toString方法,使之可以按照我們要求的格式打印
79 public String toString() {
80 return "name is "+name+" age is "+ age;
81 }
82 }
83 class MyCompare implements Comparator<Employer1> {
84 @Override//重載了Comparator接口里面的compare方法實現具體的比較
85 public int compare(Employer1 o1, Employer1 o2) {
86 return o1.getAge().compareTo(o2.getAge());
87 }
88 }
89 class Employer2{
90 private String name;
91 private Integer age;
92 public void setName(String name) {
93 this.name = name;
94 }
95 public Integer getOrder() {
96 return age;
97 }
98 public void setAge(Integer age) {
99 this.age = age;
100 }
101 @Override//重載了Object類里的toString方法,使之可以按照我們要求的格式打印
102 public String toString() {
103 return "name is "+name+" age is "+age;
104 }
105 }
106 class Employer3 implements Comparable<Employer3>{
107 private String name;
108 private Integer age;
109 public void setName(String name) {
110 this.name = name;
111 }
112 public Integer getAge() {
113 return age;
114 }
115 public void setAge(Integer age) {
116 this.age = age;
117 }
118 @Override//重載了Object類里的toString方法,使之可以按照我們要求的格式打印
119 public String toString() {
120 return "name is "+name+" age is "+age;
121 }
122 @Override//重載了Comparable接口里的compareTo方法來實現具體的比較
123 public int compareTo(Employer3 a) {
124 return this.age.compareTo(a.getAge());
125 }
126 }
打印的結果為:
[2, 5, 9] [name is b1 age is 33, name is a1 age is 44, name is b1 age is 55] [name is b3 age is 55, name is a3 age is 77, name is b3 age is 99] [name is b2 age is 22, name is b2 age is 33, name is a2 age is 66]
★compareTo()小結
由上面的程序我們可以看出,無論是實現了Comparable接口的方法還是實現了Comparator接口的方法,最終比較的返回值都是通過compareTo方法實現的,故就把compareTo方法單獨拿出來做個小結。
compareTo()的返回值是整型,它是先比較對應字符的大小(ASCII碼順序),如果第一個字符和參數的第一個字符不等,結束比較,返回他們之間的差值,如果第一個字符和參數的第一個字符相等,則以第二個字符和參數的第二個字符做比較,以此類推,直至比較的字符或被比較的字符有一方全比較完,這時就比較字符的長度。例如:
1 String s1 = "abc";
2 String s2 = "abcd";
3 String s3 = "abcdfg";
4 String s4 = "1bcdfg";
5 String s5 = "cdfg";
6 System.out.println( s1.compareTo(s2) ); // -1 (前面相等,s1長度小1)
7 System.out.println( s1.compareTo(s3) ); // -3 (前面相等,s1長度小3)
8 System.out.println( s1.compareTo(s4) ); // 48 ("a"的ASCII碼是97,"1"的的ASCII碼是49,所以返回48)
9 System.out.println( s1.compareTo(s5) ); // -2 ("a"的ASCII碼是97,"c"的ASCII碼是99,所以返回-2)
2. shuffle
對集合進行隨機排序
1 public static void shuffle(List<?> list) 2 3 public static void shuffle(List<?> list, Random rnd)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 List c = new ArrayList();
4 c.add("w");
5 c.add("o");
6 c.add("r");
7 c.add("l");
8 c.add("d");
9 System.out.println(c);
10 Collections.shuffle(c);
11 System.out.println(c);
12 Collections.shuffle(c);
13 System.out.println(c);
14 }
15 }
運行結果為:
[w, o, r, l, d]
[l, d, w, o, r]
[o, r, d, l, w]
3. binarySearch
查找指定集合中的元素,返回所查找元素的索引
1 public static <T> int binarySearch(List<? extends Comparable<? super T>> list, 2 T key) 3 4 public static <T> int binarySearch(List<? extends T> list, 5 T key, 6 Comparator<? super T> c)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 List c = new ArrayList();
4 c.add("w");
5 c.add("o");
6 c.add("r");
7 c.add("l");
8 c.add("d");
9 System.out.println(c);
10 int m = Collections.binarySearch(c, "o");
11 System.out.println(m);
12 }
13 }
運行結果為:[w, o, r, l, d]
注意:若查找的元素不存在,示例中的n即表示該元素最有可能存在的位置的索引。
4. max
1 public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) 2 3 public static <T> T max(Collection<? extends T> coll, 4 Comparator<? super T> comp)
前者采用Collection內含自然比較法,后者采用Comparator進行比較.
5. min
1 public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) 2 3 public static <T> T min(Collection<? extends T> coll, 4 Comparator<? super T> comp)
前者采用Collection內含自然比較法,后者采用Comparator進行比較。
6. indexOfSubList
查找subList在list中首次出現位置的索引
1 public static int indexOfSubList(List<?> source, 2 List<?> target)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 List list = Arrays.asList("one two three four five six siven".split(" "));
4 System.out.println(list);
5 List subList = Arrays.asList("three four five six".split(" "));
6 System.out.println(Collections.indexOfSubList(list, subList));
7 }
8 }
運行結果為:[one, two, three, four, five, six, siven]
7. lastIndexOfSubList
使用與上例方法的使用相同,在此就不做介紹了。
8. replaceAll
替換批定元素為某元素,若要替換的值存在剛返回true,反之返回false
1 public static <T> boolean replaceAll(List<T> list, 2 T oldVal, 3 T newVal)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 List list = Arrays.asList("one two three four five six siven".split(" "));
4 System.out.println(list);
5 List subList = Arrays.asList("three four five six".split(" "));
6 System.out.println(Collections.replaceAll(list, "siven", "siven eight"));
7 System.out.println(list);
8 }
9 }
運行結果為:
[one, two, three, four, five, six, siven]
true
[one, two, three, four, five, six, siven eight]
9. reverse()
反轉集合中元素的順序
public static void reverse(List<?> list)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 List list = Arrays.asList("one two three four five six siven".split(" "));
4 System.out.println(list);
5 Collections.reverse(list);
6 System.out.println(list);
7 }
8 }
運行結果為:
[one, two, three, four, five, six, siven]
[siven, six, five, four, three, two, one]
10. rotate
集合中的元素向后移m個位置,在后面被遮蓋的元素循環到前面來
1 public static void rotate(List<?> list, 2 int distance)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 List list = Arrays.asList("one two three four five six siven".split(" "));
4 System.out.println(list);
5 Collections.rotate(list, 1);
6 System.out.println(list);
7 }
8 }
運行結果為:
[one, two, three, four, five, six, siven]
[siven, one, two, three, four, five, six]
11. copy
將集合n中的元素全部復制到m中,並且覆蓋相應索引的元素
1 public static <T> void copy(List<? super T> dest, 2 List<? extends T> src)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 List m = Arrays.asList("one two three four five six siven".split(" "));
4 System.out.println(m);
5 List n = Arrays.asList("我 是 復制過來的哈".split(" "));
6 System.out.println(n);
7 Collections.copy(m,n);
8 System.out.println(m);
9 }
10 }
運行結果為:
[one, two, three, four, five, six, siven]
[我, 是, 復制過來的哈]
[我, 是, 復制過來的哈, four, five, six, siven]
12. swap
交換集合中指定元素索引的位置
1 public static void swap(List<?> list, 2 int i, 3 int j)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 List m = Arrays.asList("one two three four five six siven".split(" "));
4 System.out.println(m);
5 Collections.swap(m, 2, 3);
6 System.out.println(m);
7 }
8 }
運行結果為:
[one, two, three, four, five, six, siven]
[one, two, four, three, five, six, siven]
13. fill
用對象o替換集合list中的所有元素
1 public static <T> void fill(List<? super T> list, 2 T obj)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 List m = Arrays.asList("one two three four five six siven".split(" "));
4 System.out.println(m);
5 Collections.fill(m, "haha52T25xixi");
6 System.out.println(m);
7 }
8 }
運行結果為:
[one, two, three, four, five, six, siven]
[haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi]
14. nCopies
返回大小為n的List,List不可改變,其中的所有引用都指向o
1 public static <T> List<T> nCopies(int n, 2 T o)
示例:
1 public class Practice {
2 public static void main(String[] args){
3 System.out.println(Collections.nCopies(5, "haha"));
4 }
5 }
運行結果為:
[haha, haha, haha, haha, haha]

