Christophe Verré wrote:I remember reading in Effective Java that you should return an empty collection, to avoid unnecessary null checks.
Consider this usage of your API: a client calls your method to get the collection and wants to perform some tasks on each element of it. Or to check the size. Returning an empty collection is perfectly consistent with returning a collection of any other size - the size() method will return 0, the iteration will do nothing. No surprise for the client. Returning null will be much more surprising and would require additional workload on the client's side. I remember Josh Bloch saying in Effective Java or somewhere else that you should not surprise the user of your API - I agree with him 100 %
If you're concerned with unnecessary empty collection creation, Rob's tip is a way to go.