function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pradeep Musthi 9Pradeep Musthi 9 

How To remove a Sobject Record using set collection in apex

public class a{
public static void m5()
{
set<Account> st=new Set<Account>{new Account(Name='Prade')};
st.add(new Account(Name='m'));
system.debug(st);

}
}

How To remove account name with prade using remove method of set??Is It Possible
Niraj Kr SinghNiraj Kr Singh
Hi Pradeep,

You can try this way:
public class a{
    public static void m5()
    {
        set<Account> st=new Set<Account>{new Account(Name='Prade')};
        st.add(new Account(Name='m'));
        system.debug(st);
        system.debug('  Final Account: ' + removeAcc(st, 'Prade'));
    }

   public Set<Account> removeAcc(Set<Account> accountSet, String strRemoveName) {
	Set<Account> removeAccList = new Set<Account>();
	for(Account objAcc : accountSet) {
		if(objAcc.Name == strRemoveName) {
			removeAccList.add(objAcc);
		}
	}
	
	if(!removeAccList.isEmpty()){
		accountSet.removeAll(removeAccList);
	}
	
	return accountSet; // Removed name account list
}
}


Thanks
Niraj