Is there a Generics Friendly way of using Collection.EMPTY_LIST in my Java Program.
I know I could just declare one myself, but I’m just curious to know if there’s a way in the JDK to do this.
Something like users = Collections<User>.EMPTY_LIST;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By doing the following:
The type of the returned list from
Collections.emptyList();will be inferred as aStringdue to the left-hand-side of the assignment. However, if you prefer to not have this inference, you can define it explicitly by doing the following:In this particular instance, this may appear as redundant to most people (in fact, I’ve seen very little code out in the wild that makes use of explicit type arguments), however for a method with the signature:
void doStuff(List<String> users)it would be perfectly clean for one to invokedoStuff()with an explicit type argument as follows: