Application Techniques



Using Hashtables

How to use Java Hashtable objects in your code.

About this technique

Details

Category

Core Programming Techniques> Java

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on coding Java for SilverStream applications in the Programmer's Guide

This example creates a Hashtable, adds values to it, retrieves values back from it, and clears it. You can enter a key and value pair into the Hashtable. The Hashtable contents are automatically displayed in list boxes. You can then remove a selected pair, clear the entire Hashtable, or find the value associated with a selected key.

Creating a Hashtable   Top of page

The following code illustrates how to create a new Hashtable.

  // Declarations under General 
  Hashtable m_hashtable = new Hashtable(); 

Notes about the code

Adding data to a Hashtable   Top of page

The following code illustrates how to add a new key and value pair to a Hashtable. In the example, the key and value are obtained from entry fields.

  private void handle_btnAddToHashtable_actionPerformed(ActionEvent 
    evt) 
    { 
        // Get the new key 
        String sKey = fldKey.getText(); 
        // Get the new value 
        String sValue = fldValue.getText();       
        // Make sure we have a key and a value 
        if (sKey.equals("")) 
        { 
          // No key entered, so display an error message,  
          // then return 
          agDialog.showMessage("Hashtable error", "You must  
              enter a key."); 
          fldKey.requestFocus(); 
          return; 
        } 
        if (sValue.equals("")) 
        { 
          // No value entered, so display an error message,  
          // then return 
          agDialog.showMessage("Hashtable error", "You must  
             enter a value."); 
          fldValue.requestFocus(); 
          return; 
        } 
        // Add the new key/value combination to the Hashtable 
        m_hashtable.put(sKey, sValue);     
        // Add the new key to the Key list box 
        lbKeys.addItem(sKey); 
        // Add the new value to the Value list box 
        lbValues.addItem(sValue); 
       
        // Clear the Key data-entry field 
        fldKey.setText(""); 
        // Clear the Value data-entry field 
        fldValue.setText("");     
        // Set the focus to the Key data-entry field 
        fldKey.requestFocus(); 
    } 

Notes about the code

Retrieving a value from a Hashtable with a key   Top of page

The following code illustrates how to retrieve a value from a Hashtable based on the key that was stored with it.

  private void handle_btnGetValue_actionPerformed(ActionEvent evt) 
    { 
        // Get the index of the selected key in the Key list box 
        int iIndex = lbKeys.getSelectedIndex();     
        // Got a valid index? 
        if (iIndex >= 0) 
        { 
          // Yes, so get the key String 
          String sKey = lbKeys.getItemAt(iIndex); 
          // Get the value for the selected key 
          String sValue = (String) m_hashtable.get(sKey);     
          // Display the value for the selected key 
          fldGetValue.setText(sValue); 
        } 
      } 

Notes about the code

Removing selected data from a Hashtable   Top of page

This code illustrates how to remove a key and value pair from a Hashtable, based on the key.

  private void handle_btnRemove_actionPerformed(ActionEvent evt) 
    { 
        // Get the index of the selected key in the Key list box 
        int iIndex = lbKeys.getSelectedIndex();     
        // Got a valid index? 
        if (iIndex >= 0) 
        { 
          // Yes, so get the key String 
          String sKey = lbKeys.getItemAt(iIndex);         
          // Remove the selected key from the Key list box 
          lbKeys.removeItemAt(iIndex);         
          // Remove the corresponding value from the Value list box 
          lbValues.removeItemAt(iIndex);        
         // Remove the key/value pair from the Hashtable 
          m_hashtable.remove(sKey); 
        } 
     } 

Notes about the code

Clearing an entire Hashtable   Top of page

The following code illustrates how to clear all the data from a Hashtable.

  private void handle_btnClearHashtable_actionPerformed(ActionEvent evt) 
    { 
        // Remove every entry from the Key list box 
        for (int i = lbKeys.getItemCount() - 1; i >= 0; i--) 
        { 
          lbKeys.removeItemAt(i); 
        } 
       // Remove every entry from the Value list box 
        for (int i = lbValues.getItemCount() - 1; i >= 0; i--) 
        { 
          lbValues.removeItemAt(i); 
        } 
        // Clear the Hashtable 
        m_hashtable.clear(); 
    } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.