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

How to Reverse HashMap, key becomes values and vice-versa

I have a HashMap with key-values. Sometime, I need to know key for a given value. Could anyone let me know how to do that ?
0
vinay_delhi
Asked:
vinay_delhi
  • 2
1 Solution
 
applekannaCommented:
import java.util.*


  HashMap h = new HashMap();
     Iterator i = h.entrySet().iterator();
     while(i.hasNext())
     {
      Map.Entry entry = (Map.Entry)i.next();
     System.out.println(entry.getKey() + "/" + entry.getValue());
   //  if((Object) entry.getValue() == your code to compare
     }
0
 
applekannaCommented:
Assuming you have string for both key and value
     {
      Map.Entry entry = (Map.Entry)i.next();
     System.out.println(entry.getKey() + "/" + entry.getValue());
     String key =  (String) entry.getValue()
     String value = (String) entry.getValue();
   //  if(value.equals("some")
     }
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.

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