Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 109
Java - Why doesn't this JFrame work 3 61
Running JavaFX on JDeveloper 12C 1 33
JavaFX TableView not displaying correctly 3 21
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

825 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