+ Start a Discussion
naresh johnnaresh john 

how to add two lists and how to subtract two lists

I have two lists in my class. One(list1) has names of the donors of last year.Another(List2) has the names of the donors of this year. I need to find the donors who donated last year but not this year. For this I need to subtract the names of list2 from list1.

 

how to do that? I searched in apex pdf doc but did not find any info. Please help me.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
_Prasu__Prasu_

If both loops have 500+ records you should try out following code to avoid Governor limits.

 

 

List<String> lstString = new List<String> {'San Francisco', 'New York', 'Paris', 'Tokyo'};
set<String> setSTring = new Set<String>(lstString );

 

set<String> setList1 = new Set<String>(List1);

set<String> setList2 = new Set<String>(List2);

 

For(String strEle : setList2)

{

       if(setList1.contains(strEle))

               setList1.remove(strEle);

}

 

setList1 will contain donors who donated last year but not this year.

 

Bala, Am I correct ?? Is this solution more efficient ? ;)

 

 

All Answers

b-Forceb-Force

you need to use nested for loop

 

 

For(Integer i=0; i < List1.size(); i++)//first year list
{
for (Integer j=0; j< List2.size(); j++) //current Year list
{ 
objDoner1=List1.get(i);
objDoner2=List2.get(j)
if(objDoner==objDoner2) // your check logic may be different { List1.remove(i); // remove this element from list1 break; } } }

 End of the loop this List2 contains only "donors who donated last year but not this year"

 

 

Hope this will help you

 

Thanks,

Bala

_Prasu__Prasu_

If both loops have 500+ records you should try out following code to avoid Governor limits.

 

 

List<String> lstString = new List<String> {'San Francisco', 'New York', 'Paris', 'Tokyo'};
set<String> setSTring = new Set<String>(lstString );

 

set<String> setList1 = new Set<String>(List1);

set<String> setList2 = new Set<String>(List2);

 

For(String strEle : setList2)

{

       if(setList1.contains(strEle))

               setList1.remove(strEle);

}

 

setList1 will contain donors who donated last year but not this year.

 

Bala, Am I correct ?? Is this solution more efficient ? ;)

 

 

This was selected as the best answer
b-Forceb-Force

Prassanna,

 I suggest this code snippet by considering List containg some Object [ not string ]

 

Anyways ...Even I didnt see anything wrong in it :)

 

Thanks

Bala

naresh johnnaresh john

HI Bala and Prasanna,

 

   Thanks alot for your Support and contribution to my forum. I have one doubt regarding sets.

 

 

How to read the elements of a set. In lists I can read the elements of a list by "LISTNAME.GET(INDEX)".With this I can get individual elements of a list.

 

But how to read individual elements of a set. I read and searched the apex doc, but no where it is discussed.

 

Please help.

 

Thanks,

Naresh

_Prasu__Prasu_

Set does't provide such method. In that case you will need to use the code snippet provided by Bala.

_Prasu__Prasu_

Let me know if you code reach governor limits I will help you out to optimize the code.

naresh johnnaresh john

HI Prasanna,

 

   Thanks alot for your quick response.If you provide the code, that will be helpful for me in future also.Not only for me it will be helpful for many.

 

Thanks,

Naresh.B

Marc C.Marc C.

list1.addAll(list2);