By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

How to use Set in Salesforce

Set is a collection of similar elements, and they are unordered. Elements can be of any data type. Set doesn't allow any duplicate values. We can't access a set element at a specific index, and we can only iterate over set elements.

Syntax:

 Creating a set:

     Set<DataType> variable name=new Set<DataType>();

We can assign the values statically at the time of creating a Set,
 
Ex:

    Set<String> name = new Set<String>{'John','Kelly','Mark'};

In another way, we can assign values dynamically when we are creating a Set,

Ex:

    Set<String> name = new Set<String>();

        name.add('John');
        name.add('Kelly');
        name.add('Mark'); 

Almost all the methods of List are similar with Set methods. Some of them are,

add(SetElement):

It inserts an element into the Set.

      Set<String> name = new Set<String>();

        name.add('cat');
        name.add('sri');

Note: If the given name matches to any of the name, which is already in the set, then only one is displayed (no duplicates).

addAll(fromlist|fromset):

       It inserts list of elements or set of elements into the set.

remove(Element):

       It deletes the element from the set.

removeAll(List|Set):

       It removes the elements from entire list or Set.

contains(Element):

       It checks whether the given element is in Set or not. If it is there it will return true. 

size():

      It returns the number of Elements in the Set.

isEmpty():

      It returns true if the set is empty.

Note: Above-mentioned methods are some of them and there are other methods.

Difference between List and Set:

List:

  • List accepts duplicate values.
  • It's an Ordered collection of data type.

Set:

  • Set won't accept any kind of duplicate values.
  • It's an Unordered collection of data type.
    Posted On
  • 17 April 2018
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
MongoDB CRUD operations with Python (Pymongo)

MongoDB with Python - Connection establishment, Create, Update, Retrieve and Delete operations explained with sample code.

Continue Reading...
Multifactor Authentication with Django MFA using Google Authenticator

Use Django Multi-Factor Authentication method to verify user identity with more than one authentication methods. It can be used for user login, any transactional methods ...

Continue Reading...
Full text search in mongodb

Full text search is a custom implementation created by the MongoDB developers as a specific index type

Full text search as an index type when ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties