public class SetTest
1: // Fig. 19.18: SetTest.java
2: // Using a HashSet to remove duplicates.
3: import java.util.List;
4: import java.util.Arrays;
5: import java.util.HashSet;
6: import java.util.Set;
7: import java.util.Collection;
8:
9: public class SetTest
10: {
11: private static final String colors[] = { "red", "white", "blue",
12: "green", "gray", "orange", "tan", "white", "cyan",
13: "peach", "gray", "orange" };
14:
15: // create and output ArrayList
16: public SetTest()
17: {
18: List< String > list = Arrays.asList( colors );
19: System.out.printf( "ArrayList: %s\n", list );
20: printNonDuplicates( list );
21: } // end SetTest constructor
22:
23: // create set from array to eliminate duplicates
24: private void printNonDuplicates( Collection< String > collection )
25: {
26: // create a HashSet
27: Set< String > set = new HashSet< String >( collection );
28:
29: System.out.println( "\nNonduplicates are: " );
30:
31: for ( String s : set )
32: System.out.printf( "%s ", s );
33:
34: System.out.println();
35: } // end method printNonDuplicates
36:
37: public static void main( String args[] )
38: {
39: new SetTest();
40: } // end main
41: } // end class SetTest
42:
43:
44: /**************************************************************************
45: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
46: * Pearson Education, Inc. All Rights Reserved. *
47: * *
48: * DISCLAIMER: The authors and publisher of this book have used their *
49: * best efforts in preparing the book. These efforts include the *
50: * development, research, and testing of the theories and programs *
51: * to determine their effectiveness. The authors and publisher make *
52: * no warranty of any kind, expressed or implied, with regard to these *
53: * programs or to the documentation contained in these books. The authors *
54: * and publisher shall not be liable in any event for incidental or *
55: * consequential damages in connection with, or arising out of, the *
56: * furnishing, performance, or use of these programs. *
57: *************************************************************************/