Solved

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

Posted on 2003-11-06
4
2,457 Views
Last Modified: 2010-07-27
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
Comment
Question by:vinay_delhi
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
applekanna earned 50 total points
ID: 9692804
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
 
LVL 3

Expert Comment

by:applekanna
ID: 9692813
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

685 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