Explore BrainMass

Java Hashtables

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

We are working on hashtables and I am trying to figure out a real life example that could be solved with hashtables to jtree as well as some type of array or jlist. I just need some opinions or examples of this part please.

Here is an exercise I do not understand in this chapter.

Observe the following code, which creates a simple hashtable.
// Demonstrate a Hashtable
import java.util.*;
class HTDemo
public static void main(String args[])
Hashtable<String,Integer> numbers = new

numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));


Starting with this provided code, add the following functionality:
1. Add the numbers 3 through 10 to the hashtable continuing in the same manner as shown.
2. Prompt the user for a string, and display the corresponding number. For example, if the user types "five", the program would output "5". This must be done using the hashtable as created in the previous step.
3. Using a loop and a single printlng statement, display all of the values (both strings and integers) in a table.

© BrainMass Inc. brainmass.com October 25, 2018, 8:12 am ad1c9bdddf

Solution Preview

The first part of this solution requires adding additional "add()"
method calls as follows:

numbers.put("three", new Integer(3));
numbers.put("four", new Integer(4));
numbers.put("five", new Integer(5));
numbers.put("six", new Integer(6));
numbers.put("seven", new Integer(7));
numbers.put("eight", new Integer(8));
numbers.put("nine", new ...

Solution Summary

This solution provides a simple example of using a HashTable. It shows how to insert elements into a hash table. It also shows how to use an Enumeration to iterate over all of the elements of the table.