You need to sign in to do that
Don't have an account?
jayam
comparison between two lists
i have two sets with string values
now i want to compare the first set to the other and the value which is there in the second set and not in first set should be added to the another list
how can i do this
plz help me
my code is like this
for(integer i=1;i<s1.size();i++)
{
if((city.contains(s1[i]) == false)
lstobj.add(s1[i]);
}
here s1 and city are sets and lstobj is list object.
Thanks in advance.....................
Think you will need something like below. What it is doing is for each item in list2 it is going through all of list1. If it finds the same value it sets isThisInList1 to true. After going through each value, if our boolean is still false, meaning we didn't find the value in list1 then add it to ValuesNotFoundInList1. Then last before we start with our next list2 value make sure the boolean is false. I apologize for any mistakes, and there is probably a more elegant solution but this should work.
You are indeed trying too elegantly. You can do this:
No need to do loops or anything fancy. You can iterate through the set in a for loop, or copy it back into a list when you're done. Either way, the platform already has the logic you need to quickly and easily determine which items in the list do not belong.
Slick.
Thanks for ur reply
but
the first list is sobject type ,it contains the list of names
and the second list is string type
when i compare those two, error came
what can i do?
You might need to tweak this code to suit your specific scenario, but I believe that a Map should be able to accomplish your goals in a similar fashion as the previous example; SObjects are complex objects, and so you have to map them to a simple type if you want to compare them quickly using the methods available to sets.