Solved

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

Posted on 2003-11-06
4
2,477 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
[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
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.
Suggested Courses

737 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