// // Example of using some Set classes. // // Usage: // java TrySet s1 s2 ... // where s1, s2, etc., are one or more strings. // // Description: // Builds both a HashSet (unsorted) and a TreeSet (sorted) // containing s1, s2, etc.; uses Iterators to print contents. // // Example of use: // java TrySet abcd 1234 xy hello xy abcd // (note behavior with duplicates; contrast with TryList and TryMap) // import java.util.*; public class TrySet { public static void main(String[] args) { Set s1 = new HashSet(); SortedSet s2 = new TreeSet(); for (int i = 0; i < args.length; ++i) { s1.add(args[i]); s2.add(args[i]); } System.out.println("contents of Set:"); Iterator iter; iter = s1.iterator(); while (iter.hasNext()) System.out.println(iter.next()); System.out.println("contents of SortedSet:"); iter = s2.iterator(); while (iter.hasNext()) System.out.println(iter.next()); } }