How to use Java Hashtable objects in your code.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
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.
The following code illustrates how to create a new Hashtable.
// Declarations under General Hashtable m_hashtable = new Hashtable();
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(); }
getText()
to obtain the new user-entered key and value pair.
agDialog.showMessage()
is called to display an error message.
put()
.
addItem()
. This is for display purposes only and does not affect the Hashtable.
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); } }
getItemAt()
on the list box using the selected index, which can be obtained by calling getSelectedIndex()
.
get()
on the Hashtable using the key string as the argument. This will return the associated value that is stored in the Hashtable.
setText()
.
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); } }
getSelectedIndex()
is called to find out which row to remove.
getItemAt()
on the selected index to get the string for the key associated with that row.
removeItemAt()
. This is for display purposes only and does not affect the Hashtable.
remove()
, using the key string as the argument.
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(); }
for
loops to remove each row that is displayed in the key and value list boxes. The getItemCount()
method gives the number of rows to loop through.
for
loop, the index is calculated by subtracting one from the row count. The index is then passed to removeItemAt()
. Removing rows from the list boxes is for display purposes only and does not affect the Hashtable.
clear()
on it.