Skip to main content

Posts

Showing posts with the label containskey method

Map contains key is case sensitive

 Here is my previous post related to map contains method.  How contains key method works ? Does it allow partial matches or exact matches ? We know map contains key method is case sensitive. if you are create a key using First Name , Last Name and DOB to find unique Account record then system will consider 2 different record for below scenario. 1. First Name - "Capital"  Last Name- "Testing" DOB- 01/01/1989 2. First Name - "CAPITAL" Last Name - "TESTING" DOB- 01/01/1989 The work wound is  map<String,Account> map_Name_Acc = new map<String,Account>(); String sKey = ''; for(Account acc:[SELECT firstname,Lastname,DOB__c from Account limit 10]{      sKey = acc.FirstName+'_'+ acc.LastName+'_'+acc.DOB__c; // this will create duplicate records since key is case sensitive  sKey = acc.FirstName.toLowercase()+'_'+ acc.LastName.toLowercase()+'_'+acc.DOB__c;  } toLowercase() - convert name to lower case, ...

How contains key method works ? Does it allow partial matches or exact matches ?

This might be very simple, Even I was also thinking as the method name is "containskey" then it should return true if partial matches, unfortunately it is not like that. "Containskey" method will return true only if key is exact match. It is also case sensitive. Below is some code snippet map<string,string> test = new map<string,string>(); test.put('abc123','GM'); system.debug('Exact matches -abc123 -'+test.ContainsKey('abc123'));---- True system.debug('Partial matches-123--'+test.ContainsKey('123')); --- False system.debug('Partial matches-abc--'+test.ContainsKey('abc')); --- False system.debug('Case sensitive matches-ABC123-- '+test.ContainsKey('ABC123')); --- False