Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashMap Vs TreeMap 12 55
java  and programming certification ? 4 86
Java and GPO 11 68
fibonacci ten numbers 4 26
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now