You need to sign in to do that
Don't have an account?

remove duplicate from list of object
Hi how can i remove duplicate from list of object ! i try like this but it's doesn't work
my list is like this :
public static List<State__c> getState(){ List <State__c> first = [SELECT Id,Name,Child__r.Name FROM State__c]; List<State__c> uniqueValue = new List<State__c>(); Set<State__c> myset = new Set<State__c>(); for (State__cs : first) { if (myset.add(s)) { uniqueValue.add(s); } } return uniqueValue; }
my list is like this :
suucces[{"Id":"a0b5E000002VSuXQAW","Name":"Abbes","Child__c":"a0Z5E000002s12BUAQ","Child__c":{"Name":"Abbes","Id":"a0Z5E000002s12BUAQ"},"Public__c":true},{"Id":"a0b5E000002VSajQAG","Name":"Abbes","Child__c":"a0Z5E000002s12BUAQ","Child__r":{"Name":"Abbes","Id":"a0Z5E000002s12BUAQ"},"Public__c":true}]
Set<sobject> myset = new Set<sobject>();
List<sobject> result = new List<sobject>();
myset.addAll(originalList);
result.addAll(myset);
You can do this by using Sets, by definition, contain no duplicates. So, the first addAll() causes any dupes to overwrite themselves when going into the set. The second addAll() just gets you back to a list. If you don't need the result to be a list, you can omit that.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.