In the HashMap, we know the values but we dont know the key,
then how can we get the key from HashMap ?????

Answers were Sorted based on User's Feedback



In the HashMap, we know the values but we dont know the key, then how can we get the key from HashM..

Answer / ganesh

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapEntrySetDemo {

public static void main(String[] argv) {

Map map = new HashMap();

map.put("Adobe", "Mountain View, CA");
map.put("IBM", "Mountain View, CA");
map.put("Learning Tree", "Los Angeles, CA");
map.put("Microsoft", "Redmond, WA");
map.put("Netscape", "Mountain View, CA");
map.put("O'Reilly", "Sebastopol, CA");
map.put("Sun", "Mountain View, CA");

Set entries = map.entrySet();
Iterator it = entries.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
if("Mountain View, CA".equals(entry.getValue()))
{
System.out.println(entry.getKey() );

}
}
}
}

Is This Answer Correct ?    6 Yes 0 No

In the HashMap, we know the values but we dont know the key, then how can we get the key from HashM..

Answer / sathiyamoorthi

Hi Ganesh, Thank u for the ur very good answer...

Is This Answer Correct ?    2 Yes 0 No

In the HashMap, we know the values but we dont know the key, then how can we get the key from HashM..

Answer / ravikiran

getkeyMethodMap

Is This Answer Correct ?    0 Yes 0 No

In the HashMap, we know the values but we dont know the key, then how can we get the key from HashM..

Answer / siva thimmannagari

by using the hashcode() method

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is a char in java?

0 Answers  


What is casting ?

2 Answers  


Difference between ‘is-a’ and ‘has-a’ relationship in java?

0 Answers  


Can a boolean be null java?

0 Answers  


What is the use of an interface?

3 Answers  


what is object slice?

2 Answers   HTC, TCS,


whats the purposr of using serialization?

6 Answers  


Need to use public,static keywords in main function?

5 Answers  


we have two threads..both the threads are reading the data.. is there any need of synchronization there?...justify it?

4 Answers   IBM,


What are different types of arrays?

0 Answers  


Can constructor return value?

0 Answers  


Does list allow duplicates in java?

0 Answers  


Categories