Skip to main content
6 votes

Why is record object with different id not used as key in Map?

The code is working absolutely fine. The issue here is the system debug which prints the map. It is printing only the first value. I modified your code to also print the size and individual elements ...
RajeshShah's user avatar
  • 1,754
6 votes
Accepted

Unexpected results when using a custom class as key in Map

Aidan has already linked to the post that describes what I think is the likely cause: hashCode() is never called when adding to Maps and Sets. Running with the debug level turned down would confirm. ...
Keith C's user avatar
  • 136k
5 votes
Accepted

HashCode Deduplication?

Does Apex also have 232 possible values? (looks to me like the same max int) Yes. Can the hashCode values for the same object really change? HashCode means different things ...
sfdcfox's user avatar
  • 497k
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
Accepted

Prevent Duplicates using unique key

BUT can I rely on it ? Is there a possibility that 2 records with different keys would generate the same hash ? Yes, there is. There must be, because you're compressing more than 256 bits of data ...
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
3 votes
Accepted

SObject key of map mutated returns null but serializing returns the value

A map internally stores the data by hash codes, so it looks like this: { "12345" => [value1], "23456" => [value2], "34567" => [value3, value4] } When retrieving a value via get, it ...
sfdcfox's user avatar
  • 497k
3 votes

System.hashCode broken on Id properties of SObjects?

Update: Fixed! I just tested this in a Spring '21 sandbox and it is fixed! The Known Issue still says In Review but I assume that's because the release isn't in production instances yet. Note that ...
kacrouse's user avatar
2 votes

Methods equals() and hashCode() misbehaving in Map.containsKey()

I cannot explain your results, though see the comment I've added to the question. My attempts at trying to use value equality between SObject instances (where all the fields are compared) have never ...
Keith C's user avatar
  • 136k
2 votes
Accepted

hashCode and equals implementation not working

I believe your problem is the naming conflict in your constructor. When you use the pattern you have, your code simply reassigns the constructor parameter, rather than the object attribute. If they ...
Adrian Larson's user avatar
  • 151k
2 votes

Salesforce why hashcode method is not working

You need the equals and hashCode implementations to be compatible, and in your implementation they are not. Both play a part in how a Map is managed. First the hashCode is used to identify the "...
Phil W's user avatar
  • 37k
2 votes

Salesforce why hashcode method is not working

You're factoring email into the hash code, which is different for each user. Therefore, you end up with set that has two items. hashCode() is called first, then equals is called afterwards if there's ...
sfdcfox's user avatar
  • 497k
2 votes

Implementing hashCode() with more than two variables

To comment on and extend the answer from @sfdcfox, I recommend against conversion to a string in order to hash, especially if your object will be used in a Set or Map in code that is executed often, ...
Phil W's user avatar
  • 37k
2 votes
Accepted

Implementing hashCode() with more than two variables

The hashCode() value isn't the only factor involved, it also checks equals(), but be aware of choosing a bad hash. It should probably be sufficient to say: return ( firstName+' '+ lastName+' '+ ...
sfdcfox's user avatar
  • 497k
1 vote

when does the Map return null?

When using custom classes to define keys, you need both an equals() method and a hashCode() method in order to ensure that the key can be found. The latter identifies the correct "bucket" in ...
sfdcfox's user avatar
  • 497k
1 vote

How do i get parameter values in decrypted format where the URL parameters are in encrypted format

"Encrypted" is not a format. Data is encrypted using a specific algorithm, key, initialization vector, mode of operation, and so forth. These values are not interchangeable. You must know exactly how ...
David Reed's user avatar
  • 93.2k

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