When I change an object in a set to match the other object in the set do they now merge?
When I have a set of 5 sobjects, and they are all unique objects (unique Signatures) I add them to a Set, giving me set.size() = 5
Then I edit a field on one of those objects to be the same, so that now 2 of those object signatures are exactly the same. However it does not merge them together. I now have 2 exact same items in my set.
If I set.addall(originalSet) to another set they do merge and now I have 4 items, so this proves they were exactly the same.
contact c1 = new contact(id='003000000089012', firstname='1', lastname='2', phone=null);
contact c2 = new contact(id='003000000089012', firstname='1', lastname='2');
contact c3 = new contact(id='003000000089012', firstname='1', lastname='2', phone='');
contact c4 = new contact(id='003000000089012', firstname='1', lastname='3', phone='');
contact c5 = new contact(id='003000000089012', firstname='1', lastname='3');
set<contact> contacts = new set<contact>();
contacts.add(c1);
contacts.add(c2);
contacts.add(c3);
contacts.add(c4);
contacts.add(c5);
for(contact c : contacts){
system.debug('5 Items: '+c);
}
system.debug('5 Items BREAK');
c1.phone='';
for(contact c : contacts){
system.debug('Same : '+c);
}
system.debug('Same BREAK');
contacts.add(c1);
for(contact c : contacts){
system.debug('c1 Added: '+c);
}
system.debug('c1 Added BREAK');
contact c6 = new contact(id='003000000089012', firstname='1', lastname='2', phone='');
contacts.add(c6);
for(contact c : contacts){
system.debug('New Same: '+c);
}
system.debug('New Same BREAK');
contacts.addall(contacts);
for(contact c : contacts){
system.debug('Addall: '+c);
}
system.debug('Addall BREAK');
set<contact> cnts2 = new set<contact>();
cnts2 = contacts.clone();
for(contact c : cnts2){
system.debug('Clone: '+c);
}
system.debug('Clone BREAK');
set<contact> cnts3 = new set<contact>();
cnts3.addall(contacts);
for(contact c : cnts3){
system.debug('New Addall: '+c);
}
system.debug('New Addall BREAK');
System.hashCode()
also it is said in the document that Uniqueness of all other non-primitive types is determined by comparing the objects’ fields.