1 package J2SE.Collections;
2
3 import java.util.Collections;
4 import java.util.Comparator;
5 import java.util.Iterator;
6 import java.util.LinkedList;
7
8 public class CollectionsDemo {
9
10 public static void main(String[] args) {
11 // a linkedlist
12 LinkedList ll = new LinkedList();
13 ll.add(new Integer(-8));
14 ll.add(new Integer(20));
15 ll.add(new Integer(-20));
16 ll.add(new Integer(8));
17 // a Comparator
18 //得用Collections类返回一个Comparator对象
19 Comparator r = Collections.reverseOrder();
20 // sort method
21 //利用Comparator对LinkedList象对对象进行排序
22 Collections.sort(ll, r);
23
24 Iterator li = ll.iterator();
25 System.out.print("List sorted in reverse: ");
26 while (li.hasNext())
27 System.out.print(li.next() + " ");
28 // random order
29 Collections.shuffle(ll);
30 li = ll.iterator();
31 System.out.print("List shuffled: ");
32 while (li.hasNext())
33 System.out.print(li.next() + " ");
34 System.out.println();
35 // min method & max method
36 System.out.println("Minimum: " + Collections.min(ll));
37 System.out.println("Maximum: " + Collections.max(ll));
38
39 }
40
41 }
42
1 package J2SE.Collections;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 /*
8 * Collections类是针对集合类的一个帮助的静态类,内部方法都为静态
9 */
10 public class CollectionsDemo2 {
11
12 public static void main(String[] args) {
13 double array[] = { 111, 111, 23, 456, 231 };
14 List list = new ArrayList();
15 List li = new ArrayList();
16 //initial list
17 for (int i = 0; i < array.length; i++) {
18 list.add(new Double(array[i]));
19 }
20 //initial li
21 double arr[] = { 111 };
22 for (int j = 0; j < arr.length; j++) {
23 li.add(new Double(arr[j]));
24 }
25 //add array2 to list and li
26 double array2[] = { 112, 111, 23, 456, 231 };
27 for (int i = 0; i < array.length; i++) {
28 list.add(new Double(array2[i]));
29 li.add(new Double(array2[i]));
30 }
31 //print li and list
32 System.out.println("\nthe li is :");
33 for (int i = 0; i < li.size(); i++) {
34 System.out.print(li.get(i)+"||");
35 }
36 System.out.println("\nthe list is :");
37 for (int i = 0; i < list.size(); i++) {
38 System.out.print(list.get(i)+"||");
39 }
40 //混排(shuffle)
41 System.out.println("\nthe shuffle order of list :");
42 Collections.shuffle(list);
43 for (int i = 0; i < list.size(); i++) {
44 System.out.print(list.get(i)+"||");
45 }
46 //排序(sort)
47 System.out.println("\nthe ordered list is :");
48 Collections.sort(list);
49 for (int i = 0; i < list.size(); i++) {
50 System.out.print(list.get(i)+"||");
51 }
52 //反转
53 System.out.println("\nthe reverse order of the li is :");
54 Collections.reverse(li);
55 for (int i = 0; i < li.size(); i++) {
56 System.out.print(li.get(i)+"||");
57 }
58 System.out.println("==============================");
59 System.out.println("\nthe li is :");
60 for (int i = 0; i < li.size(); i++) {
61 System.out.print(li.get(i)+"||");
62 }
63 //移动所有元素
64 System.out.println("\nthe new order of the li is :");
65 Collections.rotate(li,-2);//向左移动2个位置
66 for (int i = 0; i < li.size(); i++) {
67 System.out.print(li.get(i)+"||");
68 }
69 System.out.println("==============================");
70 // 替换所有元素
71 System.out.println("\nthe new li is :");
72 Collections.fill(li, new Double(2.2));
73 for (int i = 0; i < li.size(); i++) {
74 System.out.print(li.get(i)+"||");
75 }
76 System.out.println();
77 //print the biggest value
78
79 System.out.println( Collections.max(list));
80 for (int i = 0; i < list.size(); i++) {
81 System.out.print(list.get(i)+"||");
82 }
83 }
84
85 }
86