Collection in Salesforce:
- Always prefer to use Map :
Syntax
:
Map
<
Integer
,
String
> mapData =
new
Map
<
Integer
,
String
>();
- Methods :
- clear()
Removes all of the key-value mappings from the map. - clone()
Makes a duplicate copy of the map. - containsKey(key)
Returns true if the map contains a mapping for the specified key. - deepClone()
Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values. - equals(map2)
Compares this map with the specified map and returns true if both maps are equal; otherwise, returns false. - get(key)
Returns the value to which the specified key is mapped, or null if the map contains no value for this key. - getSObjectType()
Returns the token of the sObject type that makes up the map values. - hashCode()
Returns the hashcode corresponding to this map. - isEmpty()
Returns true if the map has zero key-value pairs. - keySet()
Returns a set that contains all of the keys in the map. - put(key, value)
Associates the specified value with the specified key in the map. - putAll(fromMap)
Copies all of the mappings from the specified map to the original map. - putAll(sobjectArray)
Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>. - remove(key)
Removes the mapping for the specified key from the map, if present, and returns the corresponding value. - size()
Returns the number of key-value pairs in the map. - values()
Returns a list that contains all the values in the map. - Set in Salesforce :
- Declaration :
Set
<
String
> setData =
new
Set
<
String
>();
- Methods :
- add(setElement)
Adds an element to the set if it is not already present. - addAll(fromList)
Adds all of the elements in the specified list to the set if they are not already present. - addAll(fromSet)
Adds all of the elements in the specified set to the set that calls the method if they are not already present. - clear()
Removes all of the elements from the set. - clone()
Makes a duplicate copy of the set. - contains(setElement)
Returns true if the set contains the specified element. - containsAll(listToCompare)
Returns true if the set contains all of the elements in the specified list. The list must be of the same type as the set that calls the method. - containsAll(setToCompare)
Returns true if the set contains all of the elements in the specified set. The specified set must be of the same type as the original set that calls the method. - equals(set2)
Compares this set with the specified set and returns true if both sets are equal; otherwise, returns false. - hashCode()
Returns the hashcode corresponding to this set and its contents. - isEmpty()
Returns true if the set has zero elements. - remove(setElement)
Removes the specified element from the set if it is present. - removeAll(listOfElementsToRemove)
Removes the elements in the specified list from the set if they are present. - removeAll(setOfElementsToRemove)
Removes the elements in the specified set from the original set if they are present. - retainAll(listOfElementsToRetain)
Retains only the elements in this set that are contained in the specified list. - retainAll(setOfElementsToRetain)
Retains only the elements in the original set that are contained in the specified set. - size()
Returns the number of elements in the set (its cardinality). - DrawBack :
- Set cannot be the parameter for aura enable methods in salesforce.
- Resolution :
- Add all set element to your List using addAll method.
- List in Salesforce :
- Declaration :
List
<
Integer
> ls1 =
new
List
<
Integer
>();
Methods :
- add(listElement)
Adds an element to the end of the list. - add(index, listElement)
Inserts an element into the list at the specified index position. - addAll(fromList)
Adds all of the elements in the specified list to the list that calls the method. Both lists must be of the same type. - addAll(fromSet)
Add all of the elements in specified set to the list that calls the method. The set and the list must be of the same type. - clear()
Removes all elements from a list, consequently setting the list's length to zero. - clone()
Makes a duplicate copy of a list. - contains(listElement)
Returns true if the list contains the specified element. - deepClone(preserveId, preserveReadonlyTimestamps, preserveAutonumber)
Makes a duplicate copy of a list of sObject records, including the sObject records themselves. - equals(list2)
Compares this list with the specified list and returns true if both lists are equal; otherwise, returns false. - get(index)
Returns the list element stored at the specified index. - getSObjectType()
Returns the token of the sObject type that makes up a list of sObjects. - hashCode()
Returns the hashcode corresponding to this list and its contents. - indexOf(listElement)
Returns the index of the first occurrence of the specified element in this list. If this list does not contain the element, returns -1. - isEmpty()
Returns true if the list has zero elements. - iterator()
Returns an instance of an iterator for this list. - remove(index)
Removes the list element stored at the specified index, returning the element that was removed. - set(index, listElement)
Sets the specified value for the element at the given index. - size()
Returns the number of elements in the list. - sort()
Sorts the items in the list in ascending order. - DrawBack :
- Contains() of list is not working properly in salesforce
- Resolution :
- Set a debug log for the user in your org and try it'll work.
Caution : No method can compare records of custom objects directly.
Comments
Post a Comment