• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Hash Tables

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
CSharpRules
Asked:
CSharpRules
1 Solution
 
CEHJCommented:
Enumeration e = h.keys() will return the keys
0
 
yongsingCommented:
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
 
yasser_helmyCommented:
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
 
CSharpRulesAuthor Commented:
I could not get the loop to work but knowing the syntax of
Object key = e.nextElement();
Object value = h.get(key);

0
 
yongsingCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now