What Is a SET In Apex?

Set:
Duplicate_content

              A set is an unordered collection of typed primitives, sObjects. For example, the following table is a visual representation of a set of Strings. But Sets do not allow duplicate values to be stored in them.
Set Syntax:
                  To declare a set, use the Set keyword followed by the primitive data, sObject type within "<  >" characters.
// Declaring Set
Set<datatype> setname;

//Declaring Set of String
Set<string> nameset;

//Creating an empty set of strings
Set<String> my_set = new Set<String>();

Set Methods:
ADD Method: Adds elements to a Set.
// add single ingeger to Set
Set<Integer> numSet = new Set<Integer>();
numSet.add(23);
// (OR) Add multiple elements to Set
Set<Integer> numSet = new Set<Integer>(30,20,1);

// Add strings to Set
Set<String> nameSet = new Set<String>();
nameSet.add('ramesh');
nameSet.add('Sreenu');
nameSet.add('Hari');

// Add S-objects to Set
Set<Account> accSet = new Set<Account>();
Account acc = New Account();
Acc.name = ' Nagarjun';
accSet.add(acc);

You May Also Like: What Is a Map In Apex?

CONTAINS Method: Allows us to check if an element is present in a Set or not.
Set<Integer> numSet = new Set<Integer>();
numSet.add(23);
Integer Mynum = numSet.get(0); //Retrieves first occurrence from the numSet. 

Set<String> nameSet = new Set<String>{'Nagarjun','Malli','Hari');
Boolean Result;
result = myString.contains('Malli');
system.assertEquals(result, false);

Remove Method: Retrieves element from a Set.
// Remove Integers Elements from Set
Set<Integer> numSet = new Set<Integer>(20,10,30);
numSet.remove(20);  // Removes 20 from the numSet. 

// Remove Strings Elements from Set
Set<String> nameSet = new Set<String>('Nagarjun','Malli','Hari');
NameSet.remove('Hari')  //Removes 'Hari' from the nameSet.

Clear Method: Removes all the elements from the Set and sets the size of the Set to Zero.
// Clear the elements from Set
Set<Integer> numSet = new Set<Integer>(20,10,30);
numSet.clear(); // Removes all elements from the numSet. 

Set<String> nameSet = new Set<String>('Nagarjun','Malli','Hari');
NameSet.clear() //Removes all names from the nameSet.

Clone Method: Creates a new Set and adds all the elements of an existing Set into that new Set.
// Clone the numset to newnumset
Set<Integer> numSet = new Set<Integer>(20,10,30);
Set<Integer> newnumset = numset.clone(); //Creates a duplicate copy all elements from the numSet into newnumset. 

// Clone Strings from nameset to newnam
Set<String> nameSet = new Set<String>('Nagarjun','Malli','Hari');
Set<Integer> newnameSet = nameset.clone(); //Creates a duplicate copy all elements from the nameSet into newnameset.

// Clone s-Object 
Set<Account> accSet = new Set<Account>(a1,a2,a3);
Set<Account> newaccSet = accSet.clone() //Creates a new Set newaccSet and virtually creates the new elements a1,a2,a3 into it.

IsEmpty Method: Boolean method which returns true if the Set has no elements.
// Empty the Set
Set<String> nameSet = new Set<String>{'Nagarjun','Malli','Hari'};
nameSet.clear();  //all the elements are now removed from the nameSet using the clear method, the nameSet will now be empty and hence isempty method will return true. 

system.debug('The name Set is ' + nameSet.isempty());
Result:- 13:32:49:023 USER_DEBUG [3]|DEBUG|The name Set is true

Size Method: Return the number of the elements stored in the Set.
//  Find the Set size
Set<String> nameSet = new Set<String>{'Nagarjun','Malli','Hari'}; 
Integer I = nameSet.size(); // the nameSet size is 3

system.debug('Number of elements in nameSet are' + nameSet.size());
Result:- 13:32:49:023 USER_DEBUG [3]|DEBUG| Number of elements in nameSet are 3


COMMENTS

BLOGGER
Name

Apex Apex Default Methods Apex Methods Apex_Collections Apps Batch Apex Books CRM eBooks Data Import Wizard Data Management DataLoader Dataloader.io Difference Between Error in Salesforce FAQ's FAQ's-Apex FAQ's-Apps FAQ's-CRM FAQ's-Data loader FAQ's-SOQL & SOSL FAQ'S-Triggers FAQ's-User Profile & Security FAQ's-VF Force.com Explorer Force.com Ide Formulas & Functions Integration Interview Questions Only Latest_Updates Limits&Best Practices Online-Training Reports and Dashboards Salesforce Deployment Salesforce Realtime Examples On Development Salesforce Realtime Task On Admin Salesforce Realtime Tasks Salesforce Realtime Tasks- Apex Salesforce Training Salesforce1 Mobile Sites SOQL Spring'14 Release Triggers User Profile & Security VF Tags VisualForce Winter'14 Release Wizard for Accounts/Contacts Workbench Workflows and Approvals
false
ltr
item
Sfdc Gurukul- All in one place for salesforce and force.com step by step tutorial for beginners: What Is a SET In Apex?
What Is a SET In Apex?
What Is a SET In Apex?,How to use set in apex programming,how to add elements to set,how to get element from set
http://1.bp.blogspot.com/-_Yen6bZWekQ/Uzlr1_KCPJI/AAAAAAAABpA/eXiYjGArjEc/s1600/Duplicate+content.jpg
http://1.bp.blogspot.com/-_Yen6bZWekQ/Uzlr1_KCPJI/AAAAAAAABpA/eXiYjGArjEc/s72-c/Duplicate+content.jpg
Sfdc Gurukul- All in one place for salesforce and force.com step by step tutorial for beginners
http://sfdcgurukul.blogspot.com/2013/07/what-is-set-in-apex.html
http://sfdcgurukul.blogspot.com/
http://sfdcgurukul.blogspot.com/
http://sfdcgurukul.blogspot.com/2013/07/what-is-set-in-apex.html
true
4199533888133360731
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy