Collection - Methods
All the collections in Apex have methods associated with them for assigning, retrieving, and manipulating the data. The collection methods are:
- List
- Set
- Map
The following are methods for List. All
are instance methods.
add(listElement): Adds an element to the end of the list.
Syntax: public Void add(Object listElement)
Example: List<Integer> myList = new List<Integer>();
myList.add(47);
Integer myNumber = myList.get(0);
system.assertEquals(myNumber, 47);
add(index, listElement): Inserts an element into the list at the specified index position.
Syntax: public Void add(Integer index, Object listElement)
Example: List<Integer> myList = new Integer[6];
myList.add(0, 47);
myList.add(1, 52);
system.assertEquals(myList.get(1),52);
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.
Syntax: public Void addAll(List fromList)
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.
Syntax: public Void addAll(Set fromSet)
clear(): Removes all elements from a list, consequently setting the list's length to zero.
Syntax: public Void clear()
clone(): Makes a duplicate copy of a list.
Syntax: public List<Object> clone()
equals(list2): Compares this list with the specified list and returns true if both lists are equal; otherwise, returns false.
Syntax: public Boolean equals(List list2)
get(index): Returns the list element stored at the specified index.
Syntax: public Object get(Integer index)
Example: List<Integer> myList = new List<Integer>();
myList.add(47);
Integer myNumber = myList.get(0);
system.assertEquals(myNumber, 47);
getSObjectType(): Returns the token of the sObject type that makes up a list of sObjects.
Syntax: public Schema.SObjectType getSObjectType()
hashCode(): Returns the hash code corresponding to this list and its contents.
Syntax: public Integer hashCode()
isEmpty(): Returns true if the list has zero elements.
Syntax: public Boolean isEmpty()
iterator(): Returns an instance of an iterator for this list.
Syntax: public Iterator iterator()
remove(index): Removes the list element stored at the specified index, returning the element that was removed.
Syntax: public Object remove(Integer index)
Example: List<String> colors = new String[3];
colors[0] = 'Red';
colors[1] = 'Blue';
colors[2] = 'Green';
String S1 = colors.remove(2);
system.assertEquals(S1, 'Green');
set(index, listElement): Sets the specified value for the element at the given index.
Syntax: public Void set(Integer index, Object listElement)
size(): Returns the number of elements in the list.
Syntax: public Integer size()
sort(): Sorts the items in the list in ascending order.
Syntax: public Void sort()
SET Methods:
The following are methods for Set.
add(setElement): Adds an element to the set if it is not already present.
Syntax: public Boolean add(Object setElement)
addAll(fromList): Adds all of the elements in the specified list
to the set if they are not already present.
Syntax: public Boolean addAll(List<Object> fromList)
addAll(fromSet): Adds all of the elements in the specified set to the set that
calls the method if they are not already present.
Syntax: public Boolean addAll(Set<Object> fromSet)
clear(): Removes all of the elements from the set.
Syntax: public Void clear()
clone(): Makes a duplicate copy of the set.
Syntax: public Set<Object> clone()
contains(setElement): Returns true if the
set contains the specified element.
Syntax: public Boolean contains(Object setElement)
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.
Syntax: public Boolean containsAll(List<Object> listToCompare)
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.
Syntax: public Boolean containsAll(Set<Object> setToCompare)
equals(set2): Compares this set with the specified set and
returns true if both sets are equal; otherwise,
returns false.
Syntax: public Boolean equals(Set<Object> set2)
hashCode(): Returns the hashcode corresponding to this set
and its contents.
Syntax: public Integer hashCode()
isEmpty(): Returns true if the
set has zero elements.
Syntax: public Boolean isEmpty()
remove(setElement): Removes the specified element from the set if
it is present.
Syntax: public Boolean remove(Object setElement)
removeAll(listOfElementsToRemove): Removes the elements in the specified list from
the set if they are present.
Syntax: public Boolean removeAll(List<Object> listOfElementsToRemove)
removeAll(setOfElementsToRemove): Removes the elements in the
specified set from the original set if they are present.
Syntax: public Boolean removeAll(Set<Object> setOfElementsToRemove)
retainAll(listOfElementsToRetain): Retains only the elements in
this set that are contained in the specified list.
Syntax: public Boolean retainAll(List<Object> listOfElementsToRetain)
retainAll(setOfElementsToRetain): Retains only the elements in the original set
that are contained in the specified set.
Syntax: public Boolean retainAll(Set setOfElementsToRetain)
size(): Returns the number of elements in the set (its
cardinality).
Syntax: public Integer size()
MAP Methods:
The following are methods for Map.
clear():
Removes all of the key-value mappings from
the map.
Syntax: public Void
clear()
clone():
Makes a duplicate copy of the map.
Syntax: public Map<Object, Object>
clone()
containsKey(key):
Returns true if
the map contains a mapping for the specified key.
Syntax: public Boolean containsKey(Object key)
deepClone():
Makes a duplicate copy of a map, including
sObject records if this is a map
with sObject record values.
Syntax: public Map<Object, Object>
deepClone()
equals(map2):
Compares this map with the specified map and
returns true if
both maps are
equal; otherwise, returns false.
Syntax: public Boolean equals(Map
map2)
get(key):
Returns the value to which the specified key
is mapped, or null if
the map contains no value for this key.
Syntax: public Object get(Object key)
getSObjectType():
Returns the token of the sObject type that
makes up the map values.
Syntax: public Schema.SObjectType
getSObjectType()
hashCode():
Returns the hashcode corresponding to this
map.
Syntax: public Integer hashCode()
isEmpty():
Returns true if the map has zero key-value
pairs.
Syntax: public Boolean isEmpty()
keySet():
Returns a set that contains all of the keys
in the map.
Syntax: public Set<Object>
keySet()
put(key,
value): Associates the
specified value with the specified key in the map.
Syntax: public Object put(Object key, Object value)
putAll(fromMap):
Copies all of the mappings from the specified
map to the original map.
Syntax: public Void
putAll(Map fromMap)
putAll(sobjectArray):
Adds the list of sObject records to a map
declared as Map<ID,
sObject> or Map<String, sObject>.
Syntax: public Void
putAll(sObject[] sobjectArray)
remove(key):
Removes the mapping for the specified key
from the map, if present, and
returns the corresponding value.
Syntax: public Object remove(Key
key)
size():
Returns the number of key-value pairs in the
map.
Syntax: public Integer size()
values():
Returns a list that contains all the values
in the map.
Syntax: public List<Object>
values()
Example of Map:
Thanks it helps, happy coding !!
No comments:
Post a Comment