IcedDante
IcedDante

Reputation: 6812

Best Java List implementation to return for an empty List?

Many times I'm operating in a function that returns a List type. For many types of errors or certain inputs it is invalid just to return an empty list which I normally do with the following:

return new ArrayList<DataType>();

Is that the best way or is there a better implementation of the List Interface to use for this situation? I should clarify that by "best" way I'm thinking from a performance perspective. I realize that any gains would be rather trivial but the OCD side of me is curious.

Upvotes: 3

Views: 620

Answers (1)

Alexis King
Alexis King

Reputation: 43842

There is a method of Collections specifically for this: Collections.emptyList(). This will return an empty, immutable List. You could replace the line in your code with this equivalent:

return Collections.<DataType>emptyList();

Upvotes: 14

Related Questions