Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1194
  • Last Modified:

HashMap Keys and Values

I'm trying to get the keys with corresponding values out of a HashMap.  It's easy to get the values, but how do I get the corresponding key?

Here's my code -- assume the hashmap has been filled.

HashMap hm;

Iterator it = hm.values().iterator();
while (it.hasNext()) {

    String value = (String) it.next();
}
0
lcor
Asked:
lcor
  • 2
2 Solutions
 
CEHJCommented:
Iterator it = hm.entrySet().iterator();
while (it.hasNext()) {
    Entry e = (Entry)it.next();
    String key = e.getKey().toString();
    String value = e.getValue().toString();
}
0
 
objectsCommented:
iterate on the key:

Iterator it = hm.keySet().iterator();
while (it.hasNext()) {

    String key = (String) it.next();
    String value = (String) hm.get(key);
}
0
 
CEHJCommented:
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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