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: // Map interface declarations 002: int size(); 003: boolean isEmpty(); 004: boolean containsKey(Object key); 005: boolean containsValue(Object value); 006: Object get(Object key); 007: Object put(Object key, Object value); 008: Object remove(Object key); 009: void putAll(Map t); 010: void clear(); 011: public Set keySet(); 012: public Collection values(); 013: public Set entrySet(); 014: boolean equals(Object o); 015: int hashCode();Return to top
001: // SortedMap interface declarations 002: Comparator comparator(); 003: SortedMap subMap(Object fromKey, Object toKey); 004: SortedMap headMap(Object toKey); 005: SortedMap tailMap(Object fromKey); 006: Object firstKey(); 007: Object lastKey();Return to top
001: // HashMap constructors and method 002: public HashMap(); 003: public HashMap(Map t); 004: public HashMap(int initialCapacity); 005: public HashMap(int initialCapacity, float loadFactor); 006: public Object clone();Return to top
001: import java.util.*; 002: import java.io.*; 003: 004: class SymbolMap { 005: 006: // Display a Map container's keys and values 007: public static void showMap(Map m) { 008: Iterator I = m.entrySet().iterator(); 009: while (I.hasNext()) { 010: // Get next Map.Entry object 011: Map.Entry entry = (Map.Entry)I.next(); 012: System.out.println( 013: entry.getKey() + "/t:: " + entry.getValue()); 014: } 015: } 016: 017: public static void main(String args[]) { 018: // Create a HashMap container and insert some associations 019: HashMap symbols = new HashMap(); 020: symbols.put(new Integer( 34), "Double quote"); 021: symbols.put(new Integer( 37), "Percent"); 022: symbols.put(new Integer( 38), "Ampersand"); 023: symbols.put(new Integer( 60), "Less than"); 024: symbols.put(new Integer( 62), "Greater than"); 025: symbols.put(new Integer(162), "Cent"); 026: symbols.put(new Integer(163), "Pound"); 027: symbols.put(new Integer(169), "Copyright"); 028: symbols.put(new Integer(247), "Divide"); 029: 030: // Print database or search for requested key 031: if (args.length == 0) { 032: showMap(symbols); 033: } else { 034: int key = Integer.parseInt(args[0]); 035: String value = (String)symbols.get(new Integer(key)); 036: if (value == null) 037: System.out.println(key + " not in symbols"); 038: else 039: System.out.println(key + " == " + value); 040: } 041: } 042: }Return to top
001: // Map.Entry interface 002: public interface Entry { 003: Object getKey(); 004: Object getValue(); 005: Object setValue(Object value); 006: boolean equals(Object o); 007: int hashCode(); 008: }Return to top
001: // TreeMap constructors 002: public TreeMap(); 003: public TreeMap(Comparator c); 004: public TreeMap(Map m); 005: public TreeMap(SortedMap m);Return to top
001: import java.util.*; 002: import java.io.*; 003: 004: class Dictionary { 005: 006: // Construct TreeMap dictionary container 007: static TreeMap dict = new TreeMap(); 008: 009: // Lookup and show definition for the specified word (key) 010: static void showDefinition(String word) { 011: LinkedList defs = (LinkedList)dict.get(word); 012: if (defs == null) return; // Ignore if not there 013: ListIterator L = defs.listIterator(); 014: int count = 1; // Definition counter 015: System.out.println("/n" + word); 016: while (L.hasNext()) { 017: String definition = (String)L.next(); 018: System.out.println(count++ + ". " + definition); 019: } 020: } 021: 022: // Display entire dictionary 023: static void showDictionary() { 024: Iterator I = dict.keySet().iterator(); 025: while (I.hasNext()) 026: showDefinition((String)I.next()); 027: } 028: 029: // Add a new word and/or definition 030: static void addWord(String word, String definition) { 031: if (dict.containsKey(word)) { 032: LinkedList defs = (LinkedList)dict.get(word); 033: defs.add(definition); // Add new definition only 034: } else { 035: LinkedList defs = new LinkedList(); // New list 036: defs.add(definition); // Add definition to new list 037: dict.put(word, defs); // Add word and defs association 038: } 039: } 040: 041: public static void main(String args[]) { 042: 043: // Read words and definitions into the container 044: try { 045: FileReader fr = new FileReader("Dictionary.txt"); 046: BufferedReader br = new BufferedReader(fr); 047: String word = br.readLine(); 048: while (word != null) { 049: addWord(word, br.readLine()); // Add word and definition 050: br.readLine(); // Skip blank line 051: word = br.readLine(); // Read next word 052: } 053: } catch (FileNotFoundException e) { 054: System.out.println("File not found: " + e.getMessage()); 055: } catch (IOException e) { 056: System.out.println("I/O error: " + e.getMessage()); 057: } 058: 059: // Look up one word or show entire dictionary 060: if (args.length == 0) 061: showDictionary(); // Show all 062: else 063: showDefinition(args[0]); // Show selection 064: 065: } // main 066: } // classReturn to top