SET in Apex Salesforce Introduction
Set:
It is an unordered collection of elements where elements can be of any data type.Primitive types, and built – in apex types.
1. Set don’t allow the duplicate values.
2. Insertion order is not preserved in the set.
3. It grows dynamically at run time.
Ex:
Set <string> names = new set<string> ();
Set <account> acc = new set <account> ();
Set <costumer – c> my costumers = new set <costumer—c> ();
Public void add (object) // this method will add elements to the set.
Set <string> names = new set<string> ();
name. add (‘one’);
name. add (‘two’);
name. add (‘one’);
Note: set will not allow duplicates, but if we insert it will not raise any error it will not takes values
Public void add all (list) //this method will add all elements to the set.
List <string> my list = new list <string> ();
My list. add (‘one’);
My list. add (‘two’);
Set<string> my names = new set <string> ();
My names. add (‘Sam’);
My names. add (my list);
Public integer size ()// this method will return no. of elements in the set.
Set <string> names = new set <string> ();
names. add (‘one’);
names. add (‘two’);
names. add (‘one’);
Integer my size = names. size (); // this will return 3 and stored to my size variable.
Public void removes (integer index)// this method will remove the elements at the specified index.
names. remove (1);
No comments:
Post a Comment