Results 1 to 1 of 1
-
Demonstration of the Hashtable class, and an Enumeration
Java Code:import java.util.Enumeration; import java.util.Hashtable; /** * Demonstrate the Hashtable class, and an Enumeration. * * @see HashMapDemo, for the newer HashMap class. */ public class HashtableDemo { public static void main(String[] argv) { // Construct and load the hash. This simulates loading a // database or reading from a file, or wherever the data is. Hashtable h = new Hashtable(); // The hash maps from company name to address. // In real life this might map to an Address object... h.put("Adobe", "Mountain View, CA"); h.put("IBM", "White Plains, NY"); h.put("Learning Tree", "Los Angeles, CA"); h.put("Microsoft", "Redmond, WA"); h.put("Netscape", "Mountain View, CA"); h.put("O'Reilly", "Sebastopol, CA"); h.put("Sun", "Mountain View, CA"); // Two versions of the "retrieval" phase. // Version 1: get one pair's value given its key // (presumably the key would really come from user input): String queryString = "O'Reilly"; System.out.println("You asked about " + queryString + "."); String resultString = (String) h.get(queryString); System.out.println("They are located in: " + resultString); System.out.println(); // Version 2: get ALL the keys and pairs // (maybe to print a report, or to save to disk) Enumeration k = h.keys(); while (k.hasMoreElements()) { String key = (String) k.nextElement(); System.out.println("Key " + key + "; Value " + (String) h.get(key)); } } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Demonstration of Stack class in java.util package
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:33 PM -
TreeSet Demonstration
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:34 PM -
Using Enumeration to iterate through Hashtable
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 08:44 AM -
Using Enumeration to get items out of the session
By Java Tip in forum Java TipReplies: 0Last Post: 01-04-2008, 09:34 AM -
Declaring Enumeration
By Java Tip in forum Java TipReplies: 0Last Post: 11-04-2007, 05:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks