You need to sign in to do that
Don't have an account?
case comments
getting the difference between two lists
I have a trigger in place to insert a list into a custom object. I first want to make sure there aren't any duplicates first before inserting. So far I have an sObject list of what's going to be inserted and another sObject list of what's already there. How can i find what the difference is between the two lists to insert?
For example, let's use the Account object. If i have the following in a List
List1
Acme, San Francisco
Acme, Los Angeles
IBM, New York
List2
Acme, San Francisco
Acme, Los Angeles
Acme, New York
IBM, New York
Out of those two lists, how can I pull out only Acme, New York?
For example, let's use the Account object. If i have the following in a List
List1
Acme, San Francisco
Acme, Los Angeles
IBM, New York
List2
Acme, San Francisco
Acme, Los Angeles
Acme, New York
IBM, New York
Out of those two lists, how can I pull out only Acme, New York?
You might do something like (pseudo code)
Set<String> findDifferent(List<String> list1, List<String> list2)
{
Set<String> differences = new Set<String>();
differences.addAll(list1);
for(String item : list2) //This assumes list2 doesn't not contain dupes, if it might then first convert it to a set
{
if( !differences.add(item) ) //if add returns false it means the set did NOT change, which means is already contains the item
{
differences.remove(item) //if the item was already there then remove it, it's not different...
}
//else if the item was not found, add it to differences to be returned later
}
return differences
}
The above should work...but I haven't tried it. Good luck!
I think it's feasible solution .