function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prerna BhallaPrerna Bhalla 

Difference between both list = list , list.addAll(list), list.add(listvalue)

 list<Account> stringList;
  list<Account> stringList1 = new list<Account>();
  list<Account> stringList2 = new list<Account>();
 list<Account> stringList3 = new list<Account>();
  public listTestClass() {
    stringList = new list<Account>();
    stringList= [select id, name, (select id, name from Contacts) from Account limit 2];
 
  // scenerio 1
    stringList1 = stringList;
    
//scenerio 2
    for(Account a : stringList ) {
       stringList2.add(a);
    }

//scenerio 3
stringList3.addAll(stringList);
AvaneeshAvaneesh
Hi Prerna 

That is Really a nice Scenerio and i know what u want to know ......

Scenerio 1----
Lets suppose StringList have values are {a,b}
Take one more StringListnew which have value are {a,b,c,d,e} ok

StringList1=StringList;
then StringList1 have value {a,b}
again if you do 

StringList1=StringListnew;
then StringList1 have value {a,b,c,d,e}
"Note" -- in this case common values are not added 

Scenerio 3---
Do same thing here and in this case 
"Note"--In this case common values are added so value will be {a,b,a,b,c,d,e}

Scenerio 2--
here you are iterating the value so it will be same like Scenerio-3

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

i did some change in your program and use some debug to show you the values and posted with ScreenShot
public class Problemsolution {
  list<Account> stringList;
  list<Account> stringList4;  
  list<Account> stringList1 = new list<Account>();
  list<Account> stringList2 = new list<Account>();
 list<Account> stringList3 = new list<Account>();
  public Problemsolution() {
    stringList = new list<Account>();
    stringList= [select id, name, (select id, name from Contacts) from Account limit 2];
    stringList4=[select id ,name from Account limit 5];  
      System.debug('############3stringList'+stringList);
      System.debug('!!!!!!!!!!!!!!!!11stringList4'+Stringlist4);
 
  // scenerio 1
  System.debug('##2@@@@@@@@@@@@@@@@@@stringList1'+stringList1);// null
    stringList1 = stringList;
    System.debug('##2@@@@@@@@@@@@@@@@@@stringList1'+stringList1);//objvalue
     StringList1=StringList4;
      System.debug('!!!!!!!!!!!!!!!!!!!!!!1stringList1'+StringList1);
//scenerio 2
	System.debug('##2@@@@@@@@@@@@@@@@@@stringList2'+stringList2);
    for(Account a : stringList ) {
       stringList2.add(a);
    }
System.debug('##2@@@@@@@@@@@@@@@@@@stringList2'+stringList2);
//scenerio 3
	System.debug('##2@@@@@@@@@@@@@@@@@@stringList3'+stringList3);
stringList3.addAll(stringList);
      System.debug('##2@@@@@@@@@@@@@@@@@@stringList3'+stringList3);
      System.debug('%%%%%%%%%%%%%%%%%55stringList4'+StringList4);
      Stringlist3.addAll(StringList4);
      System.debug('!!!!!!!!!!!!!!!stringList3'+StringList3);
}
}
User-added image

Note --------I hope this will helpfull Please mark as best answer ....
Thank you 
Avaneesh Singh





 
AvaneeshAvaneesh

If still any confusion Please Let me know and if this was helpfull don't forget to mark as best answer


Thank you 
Avaneesh Singh