Salesforce Collections: List, Set, Map
Collections are the group of similar types. There is no limit on the number of items a collection can hold. However, there is a general limit on heap size.
There are three types of Salesforce collections.
- List
- Set
- Map
1. List:
List is an ordered collection of elements which will allow duplicates.A list is an ordered collection of typed primitives SObjects, user-defined objects, Apex objects or collections that are distinguished by their includes.
- The index position of the first element in a list is always '0'
- List elements can be of any data type: primitive types, collections, sObjects, user-defined types, and built-in Apex types.
- A list is an ordered collection of elements that are distinguished by their indices.
- To declare a list, use the list. keyword followed by the primitive data, SObject, nested list, map, or set type within < >characters.
List Syntax:
List<datatype> listName = new List<datatype>();
Similar to lists, map values can contain any collection and can be nested within one another. A map can only contain up to five levels of nested collections inside it.
Example 1:
List<String> lstStr = new List<String>();
// If " = new List<String>() " not defined then got NullPointer error
// eg. List<String> lstStr;
// System.NullPointerException: Attemp to de-reference a null object.
// Add value into List veriable
lstStr.add('ABCD');
lstStr.add('EFGH');
lstStr.add('IJKL');
lstStr.add('MNOP');
// print List veriable
System.debug(lstStr);
// After execute this code into anonymous window
// Output: (ABCD,EFGH,IJKL,MNOP)
Example 2:
List<String> lstStr = new List<String>{'ABCD','EFGH','IJKL','MNOP'};
System.debug(lstStr);
// After execute this code into anonymous window
// Output: (ABCD,EFGH,IJKL,MNOP)
Example 3:
List<String> lstStr = [SELECT Id, Name, Amount FROM Opportunity WHERE StageName = 'Closed Won'];
System.debug(lstStr);
System.debug(lstStr.size()); // get count of elements that are present in List
// After execute this code into anonymous window
// Output: All list of opportunity having StageName = Closed Won
Example 3:
List<String> lstStr = [SELECT Id, Name, Amount FROM Opportunity WHERE StageName = 'Closed Won'];
System.debug(lstStr);
System.debug(lstStr.size()); // get count of elements that are present in List
// After execute this code into anonymous window
// Output: All list of opportunity having StageName = Closed Won
Some Example:
List<Integer> rollNumbers = new List<Integer>{1230001, 1230002, 1230003};
System.debug(rollNumbers);
// get item on index 1
Integer rollNum = rollNumbers.get(1);
System.debug(rollNum);
System.debug(rollNumbers.get(1));
// add item on index 4
rollNumbers.add(4, 2220001);
System.debug(rollNumbers);
// get the list size
System.debug(rollNumbers.size());
// remove the item on index 3
rollNumbers.remove(3);
System.debug(rollNumbers);
System.debug(rollNumbers.size());
// update item on index 1
rollNumbers.set(1, 3330001);
System.debug(rollNumbers);
// clear the list
rollNumbers.clear();
System.debug(rollNumbers);
System.debug(rollNumbers.size());
// below line will throw an error
rollNumbers.set(1, 3330001);
System.debug(rollNumbers);
# Click here : More Details about List Methods
2. Set:
Some Example:
List<Integer> rollNumbers = new List<Integer>{1230001, 1230002, 1230003};
System.debug(rollNumbers);
// get item on index 1
Integer rollNum = rollNumbers.get(1);
System.debug(rollNum);
System.debug(rollNumbers.get(1));
// add item on index 4
rollNumbers.add(4, 2220001);
System.debug(rollNumbers);
// get the list size
System.debug(rollNumbers.size());
// remove the item on index 3
rollNumbers.remove(3);
System.debug(rollNumbers);
System.debug(rollNumbers.size());
// update item on index 1
rollNumbers.set(1, 3330001);
System.debug(rollNumbers);
// clear the list
rollNumbers.clear();
System.debug(rollNumbers);
System.debug(rollNumbers.size());
// below line will throw an error
rollNumbers.set(1, 3330001);
System.debug(rollNumbers);
# Click here : More Details about List Methods
Set is an unordered collection of elements which will not allow duplicates.
A set is an unordered collection of primitives or SObjects that do not contain any duplicate elements.To declare a set, use the set keyword followed by the primitive data type name within < > characters.
- Data type allows only primitive datatypes and SObjects.
- Set elements can be of any data type: primitive types, collections, sObjects, user-defined types, and built-in Apex types.
- A set is an unordered collection of elements that do not contain any duplicates.
- We cannot get the retrieve the data based on index because set does not have index.
Set Syntax:
Set<datatype> setName = new Set<datatype>();
Some Examples:
Set<Integer> rollNumbers = new Set<Integer>{1008890, 1008100, 1007231};
System.debug(rollNumbers);
rollNumbers.add(9897767);
rollNumbers.add(9897764);
rollNumbers.add(9897765);
System.debug(rollNumbers);
// adding duplicate values - NOT ALLOWED
rollNumbers.add(9897767);
System.debug(rollNumbers);
// check if set has an item
System.debug(rollNumbers.contains(9897764));
System.debug(rollNumbers.contains(9345345));
// delete an item
rollNumbers.remove(9897765);
System.debug(rollNumbers);
// get set size
System.debug(rollNumbers.size());
// check if set is empty
System.debug(rollNumbers.isEmpty());
// remove all items
rollNumbers.clear();
System.debug(rollNumbers.isEmpty());
# Click here : More Details about Set Methods
3. Map:
Map stores key-value pairs. A map is a collection of key-value pairs where each unique key maps to a single value. Keys can be any primitive datatype while values can be primitive, sobject, collection type or Apex object.
- To declare a map, use the Map keyword followed by the data types of the value within < > characters.
- A map is a collection of key-value pairs where each unique key maps to a single value.
- Keys and values can be any data type: primitive types, collections, sObjects, user-defined types, and built-in Apex types.
- datatype_key is the storage datatype of key and it allows only primitive datatypes and should be unique.
- datatype_value is the datatype of value. Both primitive & non-primitive datatypes are allowed. Duplicates are allowed for values.
- The iteration order of map elements is deterministic. You can rely on the order being the same in each subsequent execution of the same code. However, we recommend to always access map elements by key.
- A map key can hold the null value.
- Adding a map entry with a key that matches an existing key in the map overwrites the existing entry with that key with the new entry.
- 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.
Map Syntax:
Map<datatype_key,datatype_value> mapName = new Map<datatype_key,datatype_value>();
Some Examples:
Map<Integer, String> testCls = new Map<Integer, String>();
// add a new student/item
testCls.put(12300001, 'Swapnil');
System.debug(testCls);
testCls.put(12300002, 'Harry');
testCls.put(12300003, 'Rick');
testCls.put(12300004, 'Bill');
System.debug(testCls);
testCls.put(12300005, 'Bill');
System.debug(testCls);
//update/override value
testCls.put(12300005, 'Skywalker');
System.debug(testCls);
// get a value
System.debug(testCls.get(12300003));
// remove an item from map
testCls.remove(12300002);
System.debug(testCls);
// get all the keys
Set<Integer> rollNumber = testCls.keySet();
System.debug(rollNumber);
// get all the values
List<String> students = testCls.values();
System.debug(students);
// check if map has the key
System.debug(testCls.containsKey(12300002));
System.debug(testCls.containsKey(12300003));
# Click here : More Details about Map Methods
Comments
Post a Comment