Say, I want to create a list quickly which contains 1000 elements. What is the best way to accomplish this?
Share
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.
You can use
Collections.nCopies.Note however that the list returned is immutable. In fact, the docs says “it the newly allocated data object is tiny (it contains a single reference to the data object)“.
If you need a mutable list, you would do something like
If you want 1000 distinct objects, you can use
Again, there is not guarantees about the capabilities of the resulting list implementation. If you need, say an
ArrayList, you would do