List – An ordered collection of Similar Data types. Every element in a list will be identified by a unique index. It allows duplicates

Index 0 1 2 3 4
Value Red Blue Orange White Green

List<data type>  variable;

List&lt;String&gt;strList;
strList=new List &lt;String&gt;();

// Alternate way

List &lt;String&gt; colors=new list&lt;String&gt;();
colors.add('Red');
colors.add('Blue');
colors.add('Green');
System.debug('Colors added'+colors);

Output: Colors added(Red, Blue, Green)

List allows duplicate values

List &lt;String&gt; colors=new list&lt;String&gt;();
colors.add('Red');
colors.add('Blue');
colors.add('Green');
colors.add('Red');
System.debug('Colors added'+colors);

Output: Colors added(Red, Blue, Green,Red)

// Alternate way


List&lt;String&gt; colors=new List&lt;String&gt;{'Red','Green','Blue'};

Retrieve the color from that position…

Salesforce assigns index values to the list values automatically.
List is a ordered collection.
Colors.get(2);
Colors.get(1);
String colour=colors.get(1);
System.debug('Element in 2nd position'+colour);
System.debug('Element in 1st position'+colors.get(0));
System.debug('Element in 3rd position'+colors.get(2));

How to find the size of the LIST?
ANS:
Colors.size();

integer size=colors.size();
System.debug('Size of the list'+size);

Clears the elements from the list

colors.clear();
System.debug('Elements in the list now'+colors);

List is a ordered collection, similar data type and allows duplicates

List&lt;Account&gt; accList=new List&lt;Account&gt;();
Account a1=new Account();
a1.name='AxisBank';

Account a2=new Account();
a2.name='CitiBank';

Account a3=new Account();
a3.name='HDFCBank';
accList.add(a1);
accList.add(a2);
accList.add(a3);
insert accList;
System.debug('Elements in the List'+accList);

Using a single DML, a collection of records is inserted.
Error: Too Many DMLS 151 (If we cross the governor limit of 150 DML statements)
Whenever we point to a lookup field..go for ID

Custom Object

List <Employee__c> empList=new List<Employee__c>();
Employee__c e1=new Employee__c(Name='Mark',Position_set__c='a0B6F00001QKlIx');
empList.add(e1);
Employee__c e2=new Employee__c(Name='Tom',Position_set__c='a0B6F00001QKlIx');
empList.add(e2);

insert empList;
System.debug('First Account'+accList[0]);

we can use like above instead of get.
Retrieving the values: SOQL – Salesforce Query Language should be inside [ ]

List<Account> accList=new List<Account>();
accList=[Select id,name from Account];
System.debug('Account List'+accList);

List<Account> accList=new List<Account>();
accList=[Select id,name from Account];
Account a=[Select id,name,AccountNumber,Rating from Account limit 1];
System.debug('Account List'+accList);
System.debug('Account details'+a);
System.debug('Account name'+a.Name);
System.debug('Account number'+a.AccountNumber);
System.debug('Account Rating'+a.Rating);</pre>

All the fields that are shown in output must be retrieved in SOQL first or else
the error will be thrown.

Account a=new Account(Name='Amazon web services');
insert a;

Contact c=new Contact();
c.LastName='Tst';
c.Accountid=a.id;
insert c;

SET

  • An unordered collection of similar data types
  • Set Does not allow duplicate elements.

Systax

Set<DATATYPE> variable;

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

SETS: doesnot allow duplicates, unordered collection, with similar data types

Set<data-type> variable=new Set<data-type>();
Set<String> Colorset=new Set<String>();
colorset.add('red');
colorset.add('blue');
colorset.add('green');
colorset.add('red');

System.debug('Elements in the List'+colorSet);

Output:Elements in the Set{blue, green, red}

Whenever we have to store ID’s we go for set (no dup).

clear
size allowed
get method not supported

 

MAP

MAPS: 


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

key and value are data types
Each value is identified by a key. values can be duplicate but key should be unique

Map<integer,String> StrMap=new Map<integer,String>();
StrMap.put(1,'Red');
StrMap.put(10,'Green');
StrMap.put(50,'Blue');
StrMap.put(50,'Amber');
StrMap.put(250,'Green');

System.debug('Colors in the Map'+StrMap);

Output: Colors in the Map{1=Red, 10=Green, 50=Amber, 250=Green}

Map.values -To map the values (List of values)
Map.keyset (set of keys)

System.debug('List of colors is'+StrMap.values());
System.debug('Keys in the map'+StrMap.keySet());

System.debug('Color for code 50 is'+strMap.get(50));
System.debug('Color for code 150 is'+strMap.get(150));

List <String> fruits=new List<String>{'apple','orange','pineapple','jack'};
Iterator<String> itr=fruits.iterator();
while(itr.hasnext()){
System.debug('third fruit'+itr.next());
}
for(String s:fruits)
{
   System.debug('fruit'+s) ;
}

Set <String> flowers=new Set<String>();
flowers.add('rose');
flowers.add('lilly');
flowers.add('jasmine');
System.debug('flowers'+flowers);
for(String q:flowers)
{
    System.debug('loop flowers'+q);
}
Iterator<String> w=flowers.Iterator();
while(w.hasnext())
{
    System.debug('flowers'+w.next());
}