Skip to main content
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
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
  • 151k
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
  • 497k
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
  • 151k
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
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
  • 151k
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

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
  • 497k
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
  • 497k
6 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,656
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

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

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

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

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,319
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
  • 151k
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

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
  • 93.2k
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
  • 497k
4 votes
Accepted

Put value of a field other than ID from list to set

You have to write the loop yourself e.g.: Set<String> s = new Set<String>(); for (SObject sob : Database.query(someSOQL)) s.add((String) sob.get(someField));
Keith C's user avatar
  • 136k
4 votes
Accepted

Duplicate sObjects in Set – intended behavior or bug? (API v39.0)

It works "correctly" in the SObject version, because SObject properly implements hashCode and equals. It doesn't work correctly in your custom object example, because you failed to implement hashCode ...
sfdcfox's user avatar
  • 497k
4 votes
Accepted

Need to change OwnerId field to Queue's Id if the creator of the record isn't from this Queue

You want to use groupQueue.Id instead of set recordIds public static void helper(List<Object__c> newList) { Group groupQueue = [ select Id from Group where Name = '...
Oleksandr Berehovskyi's user avatar
4 votes

Set<sobject> doesn't maintain uniqueness although I am fetching Id field

here is an example, why storing in Set sobjects is a not a best idea. Id someAcctId = '001Z000001ORdLz'; Account acct1 = [ select Id, Name from Account where Id = :someAcctId ]; ...
Oleksandr Berehovskyi's user avatar
4 votes

Would it be better to use Serialized/Deserialize or Lists for generalized Set creation?

Why not just use Map Constructor? and then use the keySet to get Set? Set<Id> resultIds = (new Map<Id,SObject>(sObjectList)).keySet();
Pranay Jaiswal's user avatar
4 votes
Accepted

Performance : Removing null from Set

Your gut feeling is somewhat correct. It's hard to get precise measurements because of the nature of how Apex executes on a shared resource, but generally speaking, if you anticipate less than about ...
sfdcfox's user avatar
  • 497k
4 votes

how to replace the value in SET in apex

First, check if it is present, second, remove found element, third, put replacement: Set<String> mySet = new Set<String>(); mySet.add('this'); mySet.add('is'); mySet.add('the'); mySet.add('...
Oleksandr Berehovskyi's user avatar

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