You need to sign in to do that
Don't have an account?
Gopikrishna Dasari
Can anyone help me in this?
I have a map like this
map<Integer,string> mapof = new map<integer,string> {1=>'Ramu',2=>'suresh'3='ramesh' 4=>'Ramu'};
I want to revrse this map
map<string,Integer> mapof1 = new map<integer,string> {'Ramu'=>1,'suresh'=>2,'ramesh' =3,'Ramu'=>4};
how can we do this?
map<Integer,string> mapof = new map<integer,string> {1=>'Ramu',2=>'suresh'3='ramesh' 4=>'Ramu'};
I want to revrse this map
map<string,Integer> mapof1 = new map<integer,string> {'Ramu'=>1,'suresh'=>2,'ramesh' =3,'Ramu'=>4};
how can we do this?
To reverse the map, you can use a for loop to iterate over the original map and add the values to a new map with the keys and values swapped.
Note that if the original map contains duplicate values, the new map will only contain the last key-value pair with that value, since maps cannot have duplicate keys.
Related:
https://stackoverflow.com/questions/4436999/how-to-swap-keys-and-values-in-a-map-elegantly
https://shreysharma.com/map-initialization-methods/
https://salesforce.stackexchange.com/questions/38767/iterating-over-a-map-apex
If this information helps, please mark the answer as best. Thank you
All Answers
To reverse the map, you can use a for loop to iterate over the original map and add the values to a new map with the keys and values swapped.
Note that if the original map contains duplicate values, the new map will only contain the last key-value pair with that value, since maps cannot have duplicate keys.
Related:
https://stackoverflow.com/questions/4436999/how-to-swap-keys-and-values-in-a-map-elegantly
https://shreysharma.com/map-initialization-methods/
https://salesforce.stackexchange.com/questions/38767/iterating-over-a-map-apex
If this information helps, please mark the answer as best. Thank you
Thanks for your answer.