Type-safe, generic, empty Collections with static generics

Posted by Droo on Stack Overflow See other posts from Stack Overflow or by Droo
Published on 2010-04-12T21:26:31Z Indexed on 2010/04/12 21:32 UTC
Read the original article Hit count: 316

Filed under:
|

I return empty collections vs. null whenever possible. I switch between two methods for doing so using java.util.Collections:

return Collections.EMPTY_LIST;
return Collections.emptyList();

where emptyList() is supposed to be type-safe. But I recently discovered:

return Collections.<ComplexObject> emptyList();
return Collections.<ComplexObject> singletonList(new ComplexObject());

etc.

I see this method in Eclipse Package Explorer:

<clinit> () : void

but I don't see how this is done in the source code (1.5). How is this magic tomfoolerie happening!!

© Stack Overflow or respective owner

Related posts about java

Related posts about collections