public class SortedSetTest
1: // Fig. 19.19: SortedSetTest.java
2: // Using TreeSet and SortedSet.
3: import java.util.Arrays;
4: import java.util.SortedSet;
5: import java.util.TreeSet;
6:
7: public class SortedSetTest
8: {
9: private static final String names[] = { "yellow", "green",
10: "black", "tan", "grey", "white", "orange", "red", "green" };
11:
12: // create a sorted set with TreeSet, then manipulate it
13: public SortedSetTest()
14: {
15: // create TreeSet
16: SortedSet< String > tree =
17: new TreeSet< String >( Arrays.asList( names ) );
18:
19: System.out.println( "sorted set: " );
20: printSet( tree ); // output contents of tree
21:
22: // get headSet based on "orange"
23: System.out.print( "\nheadSet (\"orange\"): " );
24: printSet( tree.headSet( "orange" ) );
25:
26: // get tailSet based upon "orange"
27: System.out.print( "tailSet (\"orange\"): " );
28: printSet( tree.tailSet( "orange" ) );
29:
30: // get first and last elements
31: System.out.printf( "first: %s\n", tree.first() );
32: System.out.printf( "last : %s\n", tree.last() );
33: } // end SortedSetTest constructor
34:
35: // output set
36: private void printSet( SortedSet< String > set )
37: {
38: for ( String s : set )
39: System.out.print( s + " " );
40:
41: System.out.println();
42: } // end method printSet
43:
44: public static void main( String args[] )
45: {
46: new SortedSetTest();
47: } // end main
48: } // end class SortedSetTest
49:
50: /**************************************************************************
51: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
52: * Pearson Education, Inc. All Rights Reserved. *
53: * *
54: * DISCLAIMER: The authors and publisher of this book have used their *
55: * best efforts in preparing the book. These efforts include the *
56: * development, research, and testing of the theories and programs *
57: * to determine their effectiveness. The authors and publisher make *
58: * no warranty of any kind, expressed or implied, with regard to these *
59: * programs or to the documentation contained in these books. The authors *
60: * and publisher shall not be liable in any event for incidental or *
61: * consequential damages in connection with, or arising out of, the *
62: * furnishing, performance, or use of these programs. *
63: *************************************************************************/