+ Start a Discussion
Salesforce BeginnerSalesforce Beginner 

How do I get key from a value in map

Hello Frens,

I have a map<ID,ID>. Is there any way I get the key from value ?

ex: Map<1,101>

I have access to 101 but anyway I can get its key ?
Note: values are unique too. No redundant values.
Jim RaeJim Rae
Not a good way to do this "backwards". You could loop through the values until you find the value, then use the index to get the Key from the keyset.
Mohammed MohiddinMohammed Mohiddin
for(String key : Maps.keySet()){
  if(Maps.get(key) == value){
    return key;

Sandeep Rahul PVSandeep Rahul PV
Hi Mohammed Mohiddin

I think this is a good idea. I will try.

Thank You.
Yusuf AkardereYusuf Akardere
Map<String, String> m1 = new Map<String, String>();
m1.put('a', 'First item');
m1.put('b', 'Second item');
m1.put('c', 'Third item'); 

// the value we want to get the key
String value = 'Second item';

for(String key : m1.keySet()){
  if(m1.get(key) == value){