Hashtable Using Java


The Java version of the hashtable application is simpler than the C++ version because the Java version defines the Hashtable class in the Java Collection Classes that are defined in the java.util package. The java.util package contains two classes that are designed to work with hashtables: Hashtable and HashMap class. The primary difference between them is the way they work with thread access.

The Hashtable class is a synchronized class, which means instances of the Hashtable class are safe to use for multiple thread access. The HashMap class is not synchronized and therefore is safe to use only when one thread uses an object. You can think of a thread as a process that accesses an object. Multiple processes can access an instance of the Hashtable class concurrently without any errors. However, a single process can access an instance of the HashMap class.

Let s take a look a how to create a Java version of the C++ application that you saw in the previous section of this chapter. The application is shown in the code at the end of this section. Begin by declaring an instance of the Hashtable collections class and assigning this instance to the reference called hashtable.

Next , declare two instances of the String class called key and value and assign each the first key and value that will be inserted into the hashtable. Before entering these into the hashtable, you must determine if the hashtable already contains the key. You do this by calling the containsKey() member method of the Hashtable class. This is the equivalent of the contains() member function in the C++ version of this application.

The application displays the key/value pair on the screen before calling the put() member method of the Hashtable class to insert the key/value pair into the hashtable. The put() method is the Java version of the put() member function that you built in the C++ application.

This process is repeated twice, resulting in three entries being placed into the hashtable. Here s what appears on the screen once all three key/values are in the hashtable:

 Adding node - key: 389 value: Mary Adding node - key: 415 value: Henry Adding node - key: 999 value: Joe 

Next, the application retrieves the entry that has 415 as its key. It does this by calling the get() member method of the Hashtable class and passing it the key. The get() method returns either the object containing the key/value or NULL. The return value is cast to a String object and assigned to the value s string. If the value doesn t contain NULL, the application proceeds to display the value on the screen, as shown here:

 Retrieving a value out of the hashtable: Found key: 999 value: Henry 

The application then displays the size of the hashtable and all the entries contained in the hashtable. The size of the hashtable is determined by calling the size() member method of the Hashtable class. The size is displayed on the screen, as shown here:

 Size of hashtable before removing nodes: 3 

Entries are displayed by calling the displayEntries() method. The displayEntries() method is a stand-alone method that is defined below the main() method in the application.

The displayEntries() method works by creating a key set of keys contained in the hashtable. As you ll recall from your Java programming course, a set is an object that contains a set of values that can be manipulated by an iterator. An iterator is another class that has member methods to move up and down values in the set and interact with those values.

The keySet() member method of the Hashtable class creates the set that contains keys from the hashtable. Once the set is created, the displayEntries() method creates an iterator. The hasNext() and next() member methods of the iterator step through the set of keys.

First, the hasNext() method is called within the condition expression of the while loop to determine if there is a next entry in the set. The hasNext() method returns a Boolean true if there is another entry; otherwise , a Boolean false is returned.

If there is another entry, the application calls the next() method, which moves to the next entry in the set and returns the key of that entry. The key is returned as an object, so you need to convert the object to a string, which is assigned to the key string. The key is passed to the get() method of the hashtable to retrieve the value associated with the key. The get() method returns an object that must be cast to a string so it can be assigned to the value string. Both the key and value are displayed on the screen, as shown here.

 Contents of the hashtable: key: 415 value: Henry key: 999 value: Joe key: 389 value: Mary 

Next, the application removes the entry that has 415 as its key by calling the remove() member method of the Hashtable class. After the entry is removed, the application displays the size of the hashtable and the contents of the hashtable, as shown here:

 Removing an entry from the hashtable: Size of hashtable after removing node: 2 Contents of the hashtable: key: 999 value: Joe key: 389 value: Mary 
 import java.lang.*; import java.text.*; import java.util.*; public class HashtableExample {  public static void main(String[] args)  {  Hashtable hashtable = new Hashtable();  String key;  String value;  key = "389";  value = "Mary";  if (!hashtable.containsKey(key))  {  System.out.println("Adding node - key: " + key +  " value: " + value);  hashtable.put(key, value);  }  key = "415";  value = "Henry";  if (!hashtable.containsKey(key))  {  System.out.println("Adding node - key: " + key +  " value: " + value);  hashtable.put(key, value);  }  key = "999";  value = "Joe";  if (!hashtable.containsKey(key))  {  System.out.println("Adding node - key: " + key +  " value: " + value);  hashtable.put(key, value);  }  System.out.println("\nRetrieving a value out of the  hashtable:");  value = (String)hashtable.get("415");  if(value != null)  {  System.out.println("Found key: " + key + " value:     " + value);    }   System.out.println("\nSize of hashtable before      removing nodes: " + hashtable.size());   System.out.println("\nContents of the hashtable:");   displayEntries(hashtable);   System.out.println("\nRemoving an entry from the        hashtable:");   hashtable.remove("415");   System.out.println("\nSize of hashtable after     removing node: " + hashtable.size());   System.out.println("\nContents of the hashtable:");  displayEntries(hashtable);   }  private static void displayEntries(Hashtable hashtable)  {   Set keys = hashtable.keySet();    Iterator ii = keys.iterator();   while(ii.hasNext())   {    String key = (String)ii.next();    String value = (String)hashtable.get(key);    System.out.println("key: " + key + "\tvalue: "     + value);    }  } } 



Data Structures Demystified
Data Structures Demystified (Demystified)
ISBN: 0072253592
EAN: 2147483647
Year: 2006
Pages: 90

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net