Skip to main content
2 of 3
added 58 characters in body
Eric
  • 54.4k
  • 11
  • 104
  • 196

remove duplicates from Map

I have a list which contains duplicate entries.

List<Account> aList1=new List<Account>([select Id,Name from account where Name LIKE '%Falcon%']);
Map<String,Account> aMap=new Map<String,Account>();
for(Account a:aList1)
{
    aMap.put(a.Name,a); // remove duplicate if any
}

Now I have created a trigger on account object that prevents duplicates.

Now I want to compare all values in aMap with all records in aList2. If there are any duplicates they are removed.

List<Account> aList2=[select ID,Name from Account ORDER BY Name];
for(Account a:aList2)
{
   if(aMap.containsKey(a.Name))
       aMap.remove(a.Name);  removes duplicates from aList1
}

Database.Insert(aMap.values(),false); But after the for Loop, aMap is empty.Please let me know.