Tuesday, September 15, 2020

Collection Classes in Apex Language

Collections :-

List:

-> A list is an ordered collection of elements

-> List elements can be of any data type — primitive types, collections, sObjects,user-defined types, and built-in Apex types.

-> List allows duplicate values and are referred to by their indices. Output of every SOQL(Salesforce Object Query Language) query is a list.

Syntax:-

Creating a list:List<DataType> variablename = new List<DataType>();

            Ex:

                        List<String> name = new List<String>{‘Lokesh,’SSS’,’Tirupathi’};

                        Hence values are stored as,

                        Index pos 0 = Lokesh

                        Index pos 1 = SSS

                        Index pos 2 = Tirupathi

            In other way, we can assign values dynamically when we are creating a list

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

                        name.add(‘Lokesh’);

                        name.add(‘SSS’);

                        name.add(‘Tirupathi’);

            Hence the values and index positions are stored as mentioned above.

            List has many predefined methods in which we will look into some of them.

           

            Methods :-

 

add(ListElement):-

 

it inserts an element into the list.

            List<String> listOfStrings = List<String>();

            listOfStrings.add(‘SSS’);

            listOfStrings.add(‘Tirupathi’);

 

            add(index,ListElement):-

 

it inserts the elements at the given index number.

List<String> listOfStrings = List<String>();

listOfStrings.add(‘Lokesh’);

listOfStrings.add(‘Tirupathi’);

listOfStrings.add(0,’software’);

listOfStrings.add(1,’SSS’);

Positions will be like,

Index pos 0 = software

Index pos 1 = SSS

Index pos 2 = Lokesh

Index pos 3 = Tirupathi

 

addAll(fromList):-

 

It is used to add all the elements in the list specified , and both lists should be of same type .it is represented as

Public void addAll(List fromList);

 

addAll(fromSet):-

 

It is used to add all the elements in the set when this method is called, and both set and list elements should be of same type.Represented as,

 

Public void addAll(Set fromSet);

 

Size():-

 

It is used to get the total number of elements in the list.Represented as,

Public integer size();

 

clear():-

 

It will remove all the elements from the list.Hence the length of that list will be zero.

Public void clear();

 

get(index):-

 

It returns the element which is at a given index.

Public Object get(Integer index)

 

isEmpty():-

 

It returns true if the list is empty.

Public Boolean isEmpty()

 

clone():-

 

It creates another copy of the list.

Public List<Object> clone();

 

2.Set :-

 

            Set is a collection of similar elements and they are unordered.Elements can be of any data type.

            Set doesn’t allow any duplicates values. We can’t access a set element at a specific index,and we can only iterate over set elements.

 

Syntax:

 

            Set<DataType> variableName =  new Set<DataType>();

            We can assign the values statically at the time of creating a set.

Ex:-

            Set<String> setOfStrings = new Set<String>{‘Lokesh’,’SSS’,’Tirupathi’};

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

 

Ex:-     Set<String> setOfStrings = new Set<String>();

            setOfStrings.add(‘Lokesh’);

            setOfStrings.add(‘SSS’);

            setOfStrings.add(‘Tirupathi’);

 

            Almost all the methods of the list are similar with Set methods.some of them are.

            add(SetElements)

            -> it inserts an element into the Set.

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

            setOfStrings.add(‘Lokesh’);

            setOfStrings.add(‘SSS’);

            setOfStrings.add(‘Tirupathi’);

 

Note:-

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

addAll(fromList|fromSet):-

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

remove(Element):-

It deletes the element from the set.

remove(List|Set):-

It removes the 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.

 

3.Map :-

 

Map is a collection of classes,which consists of a group of elements.

-> It is a key and value pair.

-> Each and every element in the map consists of key and value.

-> key should be unique and value can be duplicated.

-> It supports dynamic memory allocation.

-> It is like a collection of key-value pairs,where each key(unique) maps to a single value.

-> Keys and values can be any data type.

Syntax:-

----------

Map<key,value> variableName = new Map<key,value>();

In salesforce, we have standard object account ,if we want to map id with account,it is represented as,

Map<Id,Account>accMap = new Map<Id,Account>();

 

put<key,value>:

map<String,String> mapStrings = new map<String,String>();

mapStrings.put(‘red’,’shirt’);

mapStrings.put(blue,’t-shirt’);

mapStrings.put(‘black,’shirt’);

mapStrings.put(‘navy blue’,’t-shirt’);

2 comments:

  1. Informative and helpful Article. Really good work. Appreciate it. You might be looking for POS Software Development Company in Jalandhar

    ReplyDelete