Solved

Hash Tables

Posted on 2002-07-08
5
158 Views
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++){
             h.put(""+i,getPaymentType(i));
        }
     return h;
}
0
Comment
Question by:CSharpRules
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Enumeration e = h.keys() will return the keys
0
 
LVL 9

Accepted Solution

by:
yongsing earned 30 total points
Comment Utility
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
}
0
 
LVL 3

Expert Comment

by:yasser_helmy
Comment Utility
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'.
0
 

Author Comment

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

0
 
LVL 9

Expert Comment

by:yongsing
Comment Utility
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
}
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now