Solved

Hash Tables

Posted on 2002-07-08
5
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Expert Comment

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

Accepted Solution

by:
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
}
0
 
LVL 3

Expert Comment

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

Author Comment

by:CSharpRules
ID: 7157396
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
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
}
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

688 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