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
ethan huntethan hunt 

empty collection

Hi All,

Can we return empty collection in Apex as we do in Java using Collection.emptylist().

Regards
Elie.RodrigueElie.Rodrigue
You could just create another one : return new List<object>();
AshwaniAshwani
You if you are using List<T>, Map<T> or Set<T>. You can make them empty by collectionReference.clear();
Sudeep DubeSudeep Dube
collection.clear(); // may be use and then u can use this collection again
umesh atryumesh atry
No, We can not use directly Collection.
We have to Use List, set , Map
And we can return w List<object>(), w Set<object>(), m Map<Object,Object>();
Sameer PrasonnSameer Prasonn
I agree with umesh, we can return a new object of collection type i.e Set,List or Map

return new Set<object>();

or

return new Map<string, object>();

or 

return new Set<object>();

hope this resolve your query.