Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 55
Python Assistance 7 30
Eclipse IDE - Cannot copy/paste from console output 8 58
eclipse java  build path 6 16
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 …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

10 Experts available now in Live!

Get 1:1 Help Now