+ Start a Discussion
aks001aks001 

Remove duplicates from list

Hi,

 

I'm trying to remove duplicate eleements from a list using iterator and it's not allowing me to do so, saying that you can't modify the  list while iterating the same.

 

Iterator<InvoiceLineItem__c> itr = invoiceLineItems.iterator();
        
        while(itr.hasNext()){
           for (integer i = 0; i < invoiceLineItems.size(); i++){
                if (itr.next() == invoiceLineItems.get(i)){
                    invoiceLineItems.remove(i);
                }
            }
        }

 

 

If you guys knowing any of it's solutions than plz let me know. thanks in advance ................ :)

RockzzRockzz

Hi ....

 

try below code ....

 

Set<sobject> myset = new Set<sobject>();
List<sobject> result = new List<sobject>();
myset.addAll(originalList);
result.addAll(myset)
Deepa.B.AnkaliDeepa.B.Ankali
@aks001,

You are trying to remove element from the same list that is being iterated. Try iterating with a cloned list.
aks001aks001

@

aks001aks001
@Munna....

thanks but it's all the same....... do you have any other idea.....
RockzzRockzz

@ask001

 

Once u get the iterator() u can't modify the List...

 

 

Thanks,

Cool Sfdc