[SalesForce] Is Apex Sometimes Case Sensitive

I have a string set of field names populated as below:

Set<String> setAllCustomSettingFields = new Set<String>();

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
for(String objectName : gd.keySet()){
    Schema.SObjectType result = gd.get(objectName);
    if(result.getDescribe().isCustomSetting()){
        Map<String, Schema.SObjectField> objectFields = result.getDescribe().fields.getMap();
        for(String fieldString : objectFields.keySet()) {
            setAllCustomSettingFields.add(fieldString);
        }
    }
}

All the strings in the set turn out to be all lowercase. It contains, for example, 'api_url__c.'

However, setAllCustomSettingFields.contains('API_URL__c') was false, whereas setAllCustomSettingFields.contains('api_url__c') is true.

I have never seen that before, always thought all Apex is case insensitive, and the documentation on contains() doesn't say anything about it.

Best Answer

Yes. Collection membership is case sensitive.

This applies to Sets:

If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.

and to Maps:

Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put, get, containsKey, and remove treat these keys as distinct.

Related Topic