public class Algorithms2
1: // Fig. 19.15: Algorithms2.java
2: // Using algorithms addAll, frequency and disjoint.
3: import java.util.List;
4: import java.util.Vector;
5: import java.util.Arrays;
6: import java.util.Collections;
7:
8: public class Algorithms2
9: {
10: private String[] colors = { "red", "white", "yellow", "blue" };
11: private List< String > list;
12: private Vector< String > vector = new Vector< String >();
13:
14: // create List and Vector
15: // and manipulate them with methods from Collections
16: public Algorithms2()
17: {
18: // initialize list and vector
19: list = Arrays.asList( colors );
20: vector.add( "black" );
21: vector.add( "red" );
22: vector.add( "green" );
23:
24: System.out.println( "Before addAll, vector contains:" );
25:
26: // display elements in vector
27: for ( String s : vector )
28: System.out.printf( "%s ", s );
29:
30: // add elements in colors to list
31: Collections.addAll( vector, colors );
32:
33: System.out.println( "\n\nAfter addAll, vector contains: " );
34:
35: // display elements in vector
36: for ( String s : vector )
37: System.out.printf( "%s ", s );
38:
39: // get frequency of "red"
40: int frequency = Collections.frequency( vector, "red" );
41: System.out.printf(
42: "\n\nFrequency of red in vector: %d\n", frequency );
43:
44: // check whether list and vector have elements in common
45: boolean disjoint = Collections.disjoint( list, vector );
46:
47: System.out.printf( "\nlist and vector %s elements in common\n",
48: ( disjoint ? "do not have" : "have" ) );
49: } // end Algorithms2 constructor
50:
51: public static void main( String args[] )
52: {
53: new Algorithms2();
54: } // end main
55: } // end class Algorithms2
56:
57: /**************************************************************************
58: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
59: * Pearson Education, Inc. All Rights Reserved. *
60: * *
61: * DISCLAIMER: The authors and publisher of this book have used their *
62: * best efforts in preparing the book. These efforts include the *
63: * development, research, and testing of the theories and programs *
64: * to determine their effectiveness. The authors and publisher make *
65: * no warranty of any kind, expressed or implied, with regard to these *
66: * programs or to the documentation contained in these books. The authors *
67: * and publisher shall not be liable in any event for incidental or *
68: * consequential damages in connection with, or arising out of, the *
69: * furnishing, performance, or use of these programs. *
70: *************************************************************************/