dcsimg
retrieve key from value
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Amit_Kumar
Posted On:   Thursday, June 23, 2005 11:35 PM

How can I retrieve a key corrosponding to a value ?

Re: retrieve key from value

Posted By:   ankush_purwar  
Posted On:   Friday, June 24, 2005 11:53 AM

Keys are unique but two different keys may have same value.
So It might be possible corresponding the passed valyue you may get more than one keys.


For this first of all you have to override equals() method for the object that you are storing in collection.

then you can write following method ( For example you are using hashmap).

Methodm1 (Object ob){
Iterator it = hasmap.keySet().iterator();
ArrayList a = new ArrayList();
while(it.hasNext()){
Object key = it.next();
if (hashmap.get(key).equals(ob) )
a.add (key);

}
return arraylist (that contains all the keys)
}
About | Sitemap | Contact