33 votes
Accepted

When To Use Set vs. List In SOQL

(tl;dr at bottom) Filter Speed If you can use Map.keySet(), you'll get a Set back in about 1/10th of the time versus a loop over those same records and adding all the values. Of course, the Map<...
sfdcfox's user avatar
  • 491k
17 votes

Why Set<Sobject> doesn't support DML operation

One reason may be that Set<SObject> is a risky mechanism to use: equality is based on all the fields (so is expensive) and if fields are changed logic can easily break. Same problem using ...
Keith C's user avatar
  • 136k
16 votes
Accepted

Can I use set<Sobject> instead of List<Sobject> to resolve the duplicate id issue

No. Any field variation will cause the records to hash differently, even if they have the same Id. This is why Map<Id, SObject> is preferable. Id commonId = '001000000000000AAA'; Account a = ...
Adrian Larson's user avatar
  • 150k
15 votes

When To Use Set vs. List In SOQL

Update: I missed what is probably the single most important part of the question in answering this - the "In SOQL" part. Perhaps consider this answer in terms of the other processing you are going to ...
Daniel Ballinger's user avatar
11 votes
Accepted

Most Efficient Method to Convert Set Type?

Approach This should be my final update. I reran my tests, setting all Debug Levels to NONE except Apex, which I set to ERROR. The findings are more or less unchanged: casting beats all other ...
Adrian Larson's user avatar
  • 150k
9 votes
Accepted

Set performance - is using addAll and removeAll more performant than for loops?

Note that you are calculating the Set Complement. The set methods are 16x-25x faster. I ran 20 trials each with the following configurations, all with Set<Integer> of 1000 elements to generate ...
Adrian Larson's user avatar
  • 150k
9 votes
Accepted

Maps keySet is a collection that supports remove() but is otherwise read-only

One explanation may be that the underlying Java HashMap code behaves that way; this HashMap source code includes this code: 884 public Set<K> More ...keySet() { 885 Set<K> ks =...
Keith C's user avatar
  • 136k
8 votes
Accepted

Return True when set 1 contains ANY value from set 2

It's kind of awkward, but I've always just used a third set: Set<String> firstSet = new Set<String>(), secondSet = new Set<String>(), thirdSet = new Set<String>(); // ...
sfdcfox's user avatar
  • 491k
8 votes
Accepted

Generic parameter not accepted for sets?

The Set collection type does not allow you to implicitly cast, unlike with List. You can, however, overload the signature to work around the issue. You have to do some tricky maneuvering to genericize ...
Adrian Larson's user avatar
  • 150k
8 votes
Accepted

How to handle attributes of type "Set" in Lightning?

I think that the problem is in javascript when you are declaring mySet. Try this: var mySet = new Set(component.get("v.mySet")); Edit: the best solution is var changes = cmp.get("v.changes") || ...
Torsen's user avatar
  • 1,534
7 votes

How to Compare two sets namely set A and set B while both contains set of ids then I need to remove id from second set

You can use "containsAll" and "removeAll" method of set. list: Boolean containsAll = satb.containsAll(setA); This "ContainsAll" method will store true if all the elements are present in setB. ...
Nitin's user avatar
  • 1,475
7 votes
Accepted

any shortcut to get set<String> from map<Id,Set<String>> ?

You have to loop through and use addAll. There is no shortcut. Set<String> allValues = new Set<String>(); for (Set<String> subset : st.values()) { allValues.addAll(subset); }
Adrian Larson's user avatar
  • 150k
7 votes
Accepted

What happens when I edit an sobject in a Set?

Equality and the hash code value are based on all the properties of the SObject which makes sense. But when an object is placed in a hashMap, the hash code is used to determine which "bucket" the ...
Keith C's user avatar
  • 136k
6 votes
Accepted

Execution of AfterInsert caused by: System.StringException: Invalid id: () - where have I gone wrong?

When you perform a comparison between a String and an Id (e.g. ==, !=, or <>), then the other argument is "promoted" to an Id value. All Id values must be 15 or 18 characters long, and must contain ...
sfdcfox's user avatar
  • 491k
6 votes
Accepted

Retrieving values() from a Map of Sets in SOQL query

You need to first combine all the Set<Id> into one Set<Id> in Apex code: Set<Id> allCaseIds = new Set<Id>(); for (Set<Id> ids : caseIdsByFlag.values()) { allCaseIds....
Keith C's user avatar
  • 136k
6 votes

Maps keySet is a collection that supports remove() but is otherwise read-only

It's not a read-only map, per se (the error is shared with other truly read-only collections, like Trigger.new). It allows you to remove values, which also removes them from the map. You can use the ...
sfdcfox's user avatar
  • 491k
6 votes
Accepted

Converting Set to List creates duplicates of my Sobjects

Map and Set are internally based on HashMap and HashSet, respectively. In these classes, there are "buckets" created for each hashCode, and within each bucket, you have a list of values that ...
sfdcfox's user avatar
  • 491k
6 votes

Illegal assignment from List<Set<Id>> to Set<Id>

emailMatchingContacts.values() will return List of the values of Map from key-value pairs. That will be List<Set<Id>> This will be resolved by either 1 of them: emailMatchingContacts ...
salesforce-sas's user avatar
5 votes

Set removeAll is removing values from original source

Apex collections are always passed by reference meaning that it is just a pointer to the original collection. If you want to create a new instance of a collection that can be independently modified ...
dsharrison's user avatar
  • 3,992
5 votes
Accepted

List<User> returns null

Please apply system.debug('###########'+UID); before below line and check UID Is as a blank , I think UID is returning blank value in your case. Set<Id> UID = new Set <Id> (); UID.add(...
Ravikant Kedia's user avatar
5 votes

How can I check if an sObject is contained by a Set after changing a field value?

This interesting Apex oddity came up when I was debugging a problem around hash code collisions. Just by adding a System.debug statement that drops the contents of the Set it becomes possible to find ...
Daniel Ballinger's user avatar
5 votes

Doesn't Set<T> collection implement Iterable<T> interface?

Looks like Set does not implement iterable. Code: System.debug(new List<Object>() instanceof Iterable<Object>); Output: Compile failure on line 2, column 14: Operation instanceof is ...
Vladyslav K's user avatar
  • 2,743
5 votes
Accepted

Put common values of two sets within one of them without using loop

There is a standard Set method, called retainAll. That method remove all elements in original set, except from set in parameter (save in current set intersection of the two sets) and returns true if ...
kurunve's user avatar
  • 4,309
5 votes

Why Set<Sobject> doesn't support DML operation

Sets (and Maps) cannot be cast to Sets of a different type. For example, Set<sObject> t = (Set<sObject>)new Set<Contact>() will fail with the message Incompatible types since an ...
IllusiveBrian's user avatar
5 votes

Duplicate class objects in a set

For any User defined type, you need to implement the equals and hashCode methods. Refer to the below excerpt from the documentation. Uniqueness of set elements of user-defined types is determined ...
Jayant Das's user avatar
  • 30.6k
5 votes
Accepted

Adding null to Set<Id> doesn't return an empty Set

What you have observed is expected behavior: null is a valid collection element, and containing it makes the collection non-empty.
Adrian Larson's user avatar
  • 150k
5 votes

Set collection doesn't always enforce uniqueness with the Date datatype? Does the following example seem correct?

UPDATE It seems to have been reported as a known issue here. Though the title says String.valueOf but the code sample in the details reflects what is being observed here. This overall behavior is ...
Jayant Das's user avatar
  • 30.6k
5 votes
Accepted

Assigning SOQL results to a set

You can instead get the results into Map and get the keyset from Map to get Set<Id> Map has a constructor which takes List<sObject> and creates a instance of Map with ID as Key and ...
Vijay Ganji's user avatar
  • 6,606
5 votes
Accepted

How to pass set<id> dynamically to soql

You can't concatenate a Set<Id> into a string. Instead, use Apex binding. Simple Apex binds (variable names only, no complex expressions) are supported in Dynamic SOQL, and allow you to avoid ...
David Reed's user avatar
  • 92.8k
5 votes
Accepted

System.hashCode still broken for Id

Per Chris Peterson: Known. Fixed in next release. Doesn't impact apex collection correctness since they use magic to use a "proper" hashCode value thankfully. Obviously, #SafeHarbor, but ...
sfdcfox's user avatar
  • 491k

Only top scored, non community-wiki answers of a minimum length are eligible