Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

Is it more efficient to use map in apex & call .get(), or use a set and call.contains()?

Asked by David Edmunds Feb 24, 2023 894 views 2 answers
Share

About this question

Prelude

I did a couple of interwebs searches, and I used the following search terms here on SFSE...


apex map get set contains

apex map vs set

apex map set efficient*

...then looked through a number of Qs & As and some links within some of the As. If I missed the answer, please forgive and point me to it.


Background

I know just enough of Apex and other coding to be dangerous. Adhering to best practices as much as possible is preferable, but I don't know all the best practices. I also know that some debates are akin to "Toilet paper over or under?", in that there are lots of opinions but not often is there a real better answer. (There is a correct answer to the toilet paper question, btw.)


Item at hand

I was sent an Apex class with an invocable method which a different team had created and our team needs to use. One bit of functionality that it was supposed to be doing was not working as stated, and some parts seemed to me that they could use some improvement. So I modified the code, and it was sent back to the code owners for their consideration. They kept most of my changes, but a blanket statement was made that I want to know whether it is true. Not (necessarily) so that I can say that I am correct (if I am), but just to know as part of the aforementioned best practices.

Statement

Maps are more efficient [than Sets] when doing a lookup.

Question

Is it more efficient to use a map and call .get(), or use a set and call .contains()?
Code Comparison
Showing only the pertinent code (and changing names to protect the [not so] innocent), here is the original:
List groups = [SELECT Id, Name FROM Group WHERE Name LIKE :queryString];
Map groupMap = new Map();
for (Group g: groups) {
    groupMap.put(g.Name, g);
}
Group targetGroup;
String targetGroupName;
for (...) {
    targetGroupName =
    targetGroup = groupMap.get(targetGroupName);
    if(targetGroup != null){
        // Found the only one we're looking for, so set the output
        break;
    }
}
My suggested changes:
List groups = [SELECT Name FROM Group WHERE Name LIKE :queryString];
Set groupNameSet = new Set();
for (Group g: groups) {
    groupNameSet.add(g.Name);
}
String targetGroupName;
for (...) {
    targetGroupName =
    if (groupNameSet.contains(targetGroupName)) 
        // Found the only one we're looking for, so set the output
        break;
    }
}


Your answer

2 Answers

Mariakenneth Latest answer

Answered on Aug 9, 2024

Interesting discussion! It reminds me of how using maps for lookups can be like navigating through chatroulette - sometimes you find exactly what you need quickly, while other times it takes a bit longer to connect. Efficiency really matters in both cases!

Was this helpful?

More Salesforce discussions