Hash Tables

Posted on 2002-07-08
Last Modified: 2010-03-31
How can I get the key value out of this hashmap along with the values.  Currently I am trying to set the values myself but this is not working very well.

Hashtable h = com.mycompany.catalog.PaymentTypes.getTypeList();

int x=0;
Enumeration e = h.elements();
int paymentValue = 0;
int baseValue = 999;
while (e.hasMoreElements()) {
     x += 1;
     paymentValue = baseValue + x;
     cboPaymentType.insert(e.nextElement().toString (),String.valueOf(paymentValue),x);

//returning object
public static Hashtable getTypeList() {
     Hashtable h = new Hashtable();
     for (int i = 1000; i<1011; i++){
     return h;
Question by:CSharpRules
LVL 86

Expert Comment

ID: 7137989
Enumeration e = h.keys() will return the keys

Accepted Solution

yongsing earned 30 total points
ID: 7138026
Hashtable h = com.mycompany.catalog.PaymentTypes.getTypeList();

for (Enumeration e = h.keys(); e.hasMoreElements()) {
   Object key = e.nextElement(); // get the key
   Object value = h.get(key); // get the value

Expert Comment

ID: 7138237
h.keys() returns an enumeration of the keys. You can loop over it and read the keys one by one.
h.get(key) returns the value corresponding to the key 'key'.

Author Comment

ID: 7157396
I could not get the loop to work but knowing the syntax of
Object key = e.nextElement();
Object value = h.get(key);


Expert Comment

ID: 7158476
There is a mistake in my for loop. You need to place a semi-colon in it:

Hashtable h = com.mycompany.catalog.PaymentTypes.getTypeList();

for (Enumeration e = h.keys(); e.hasMoreElements();) {
  Object key = e.nextElement(); // get the key
  Object value = h.get(key); // get the value

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
difference of if loops 23 62
servlet web applications   metadata-complete="true" or false 3 109
Java string replace 11 54
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question