/Users/lyon/j4p/src/collections/sortable/CollectionsTest.java

1    /* 
2     * CollectionsTest.java 
3     * 
4     * Created on December 4, 2002 
5     */ 
6     
7    package collections.sortable; 
8     
9    import java.util.ArrayList; 
10   import java.util.Collections; 
11   import java.util.List; 
12   import java.util.ListIterator; 
13    
14   /** 
15    * Demonstrates some of the functionality available 
16    * in the Collections class. 
17    * 
18    * @author  Thomas Rowland 
19    */ 
20   public class CollectionsTest { 
21    
22       public static void main(String[] args) { 
23           List list = new ArrayList(); 
24           list.add("C"); 
25           list.add("A"); 
26           list.add("D"); 
27           list.add("B"); 
28    
29           System.out.println("\nInsertion order: "); 
30           displayList(list); 
31    
32           System.out.println("\nNatural order: "); 
33           Collections.sort(list); 
34           displayList(list); 
35    
36           System.out.println("Reverse natural order: "); 
37           Collections.sort(list, Collections.reverseOrder()); 
38           displayList(list); 
39    
40           System.out.println("Replace all B's with an X: "); 
41           // uncomment the following for jdk1.4.x 
42           //Collections.replaceAll(list, "B", "X"); 
43           displayList(list); 
44    
45       } 
46    
47       private static void displayList(List lst) { 
48           ListIterator iter = lst.listIterator(); 
49           while (iter.hasNext()) { 
50               System.out.println(iter.next()); 
51           } 
52       } 
53    
54   } 
55