You need to sign in to do that
Don't have an account?
naresh 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
If both loops have 500+ records you should try out following code to avoid Governor limits.
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
you need to use nested for loop
End of the loop this List2 contains only "donors who donated last year but not this year"
Hope this will help you
Thanks,
Bala
If both loops have 500+ records you should try out following code to avoid Governor limits.
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 ? ;)
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
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
Set does't provide such method. In that case you will need to use the code snippet provided by Bala.
Let me know if you code reach governor limits I will help you out to optimize the code.
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
list1.addAll(list2);