Home Listings By Name By Subject Email www.tomswan.com Help |
© 2001 by Tom Swan. All rights reserved.
Updated: 6/4/01 12:20:54 PM
Return to Listings page
Switch to Solutions by name page
Switch to Solutions by subject page
001: // Set interface methods 002: int size(); 003: boolean isEmpty(); 004: boolean contains(Object o); 005: Iterator iterator(); 006: Object[] toArray(); 007: Object[] toArray(Object a[]); 008: boolean add(Object o); 009: boolean remove(Object o); 010: boolean containsAll(Collection c); 011: boolean addAll(Collection c); 012: boolean retainAll(Collection c); 013: boolean removeAll(Collection c); 014: void clear(); 015: boolean equals(Object o); 016: int hashCode();Return to top
001: // SortedSet interface methods 002: Comparator comparator(); 003: SortedSet subSet(Object fromElement, Object toElement); 004: SortedSet headSet(Object toElement); 005: SortedSet tailSet(Object fromElement); 006: Object first(); 007: Object last();Return to top
001: // HashSet constructors 002: public HashSet(); 003: public HashSet(Collection c); 004: public HashSet(int initialCapacity, float loadFactor); 005: public HashSet(int initialCapacity);Return to top
001: import java.util.*; 002: import java.io.*; 003: 004: class ParseWords { 005: public static void main(String args[]) { 006: // Variables 007: int i; 008: char c; 009: StringBuffer sbuf = new StringBuffer(); 010: HashSet hashTable = new HashSet(100); 011: 012: // Read and parse words from Quote.txt 013: System.out.println(); 014: try { 015: FileReader f = new FileReader("Quote.txt"); 016: while ((i = f.read()) >= 0) { 017: c = (char)i; 018: System.out.print(c); 019: c = Character.toLowerCase(c); 020: if (Character.isWhitespace(c)) { 021: if (sbuf.length() > 0) 022: hashTable.add(sbuf.toString()); 023: sbuf.setLength(0); 024: } else 025: if (Character.isLetter(c)) 026: sbuf.append(c); 027: } 028: if (sbuf.length() > 0) 029: hashTable.add(sbuf.toString()); 030: } catch (IOException e) { 031: System.out.println("*** I/O error!"); 032: } 033: 034: // Display hash table count and contents 035: System.out.println("/n"); 036: Iterator I = hashTable.iterator(); 037: String s; 038: System.out.println("There are " + hashTable.size() 039: + " unique words in the file"); 040: while (I.hasNext()) { 041: s = (String)I.next(); 042: System.out.println(s); 043: } 044: } 045: }Return to top
001: // TreeSet constructors 002: public TreeSet(); 003: public TreeSet(Comparator c); 004: public TreeSet(Collection c); 005: public TreeSet(SortedSet s);Return to top
001: import java.util.*; 002: import java.io.*; 003: 004: class ParseTree { 005: public static void main(String args[]) { 006: // Variables 007: int i; 008: char c; 009: StringBuffer sbuf = new StringBuffer(); 010: TreeSet hashTable = new TreeSet(); 011: 012: // Read and parse words from Quote.txt 013: System.out.println(); 014: try { 015: FileReader f = new FileReader("Quote.txt"); 016: while ((i = f.read()) >= 0) { 017: c = (char)i; 018: System.out.print(c); 019: c = Character.toLowerCase(c); 020: if (Character.isWhitespace(c)) { 021: if (sbuf.length() > 0) 022: hashTable.add(sbuf.toString()); 023: sbuf.setLength(0); 024: } else 025: if (Character.isLetter(c)) 026: sbuf.append(c); 027: } 028: if (sbuf.length() > 0) 029: hashTable.add(sbuf.toString()); 030: } catch (IOException e) { 031: System.out.println("*** I/O error!"); 032: } 033: 034: // Display hash table count and contents 035: System.out.println("/n"); 036: Iterator I = hashTable.iterator(); 037: String s; 038: System.out.println("There are " + hashTable.size() 039: + " unique words in the file"); 040: while (I.hasNext()) { 041: s = (String)I.next(); 042: System.out.println(s); 043: } 044: } 045: }Return to top
001: import java.util.*; 002: import java.io.*; 003: 004: class SubTree { 005: // Display contents of a SortedSet container 006: static void showSet(SortedSet S, String msg) { 007: System.out.println("/n" + msg); 008: Iterator I = S.iterator(); 009: while (I.hasNext()) 010: System.out.print(" " + I.next()); 011: } 012: 013: public static void main(String args[]) { 014: // Create the TreeSet container and add some objects to it 015: TreeSet myTree = new TreeSet(); 016: myTree.add("Peach"); 017: myTree.add("Banana"); 018: myTree.add("Cherry"); 019: myTree.add("Apple"); 020: myTree.add("Pear"); 021: myTree.add("Kiwi"); 022: myTree.add("Grapefruit"); 023: 024: // Get a non-inclusive subset of the tree 025: TreeSet subTree = 026: (TreeSet)myTree.subSet("Cherry", "Peach"); 027: // Get an inclusive subset of the tree 028: // TreeSet subTree = 029: // (TreeSet)myTree.subSet("Cherry", "Peach/0"); 030: 031: // Display both trees 032: showSet(myTree, "Full TreeSet container:"); 033: showSet(subTree, "Subset of container:"); 034: } 035: }Return to top
001: import java.util.*; 002: import java.io.*; 003: 004: class Successor { 005: 006: // Return the successor of a SortedSet object or null if none 007: static Object successorOf(SortedSet s, Object o) { 008: SortedSet t = s.tailSet(o); 009: if (t.size() < 2) return null; 010: Iterator I = t.iterator(); 011: I.hasNext(); I.next(); I.hasNext(); 012: return I.next(); 013: } 014: 015: // Return the inclusive set of SortedSet objects o1 through o2 016: static SortedSet inclusiveSet( 017: SortedSet s, Object o1, Object o2) { 018: if (!s.contains(o1) || !s.contains(o2)) 019: throw new NoSuchElementException(); 020: Comparable c1 = (Comparable)o1; 021: Comparable c2 = (Comparable)o2; 022: if (c1.compareTo(c2) > 0) 023: throw new IllegalArgumentException(); 024: Object successor = successorOf(s, o2); 025: if (successor == null) 026: return s.tailSet(o1); 027: else 028: return s.subSet(o1, successor); 029: } 030: 031: // Display contents of a SortedSet container 032: static void showSet(SortedSet S, String msg) { 033: System.out.println("/n" + msg); 034: Iterator I = S.iterator(); 035: while (I.hasNext()) 036: System.out.print(" " + I.next()); 037: } 038: 039: public static void main(String args[]) { 040: // Create the TreeSet container and add some objects to it 041: TreeSet myTree = new TreeSet(); 042: myTree.add("Peach"); 043: myTree.add("Banana"); 044: myTree.add("Cherry"); 045: myTree.add("Apple"); 046: myTree.add("Pear"); 047: myTree.add("Kiwi"); 048: myTree.add("Grapefruit"); 049: 050: // Get a non-inclusive subset of the tree 051: TreeSet subTree1 = 052: (TreeSet)myTree.subSet("Cherry", "Peach"); 053: 054: TreeSet subTree2 = 055: (TreeSet)inclusiveSet(myTree, "Cherry", "Peach"); 056: 057: // Display all tree sets 058: showSet(myTree, "Full TreeSet container:"); 059: showSet(subTree1, "Non-inclusive subset:"); 060: showSet(subTree2, "Inclusive subset:"); 061: } 062: }Return to top