What is the difference between Collection and List in Java? When should I use which?
What is the difference between Collection and List in Java? When should I use
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.
First off: a
Listis aCollection. It is a specializedCollection, however.A
Collectionis just that: a collection of items. You can add stuff, remove stuff, iterate over stuff and query how much stuff is in there.A
Listadds the information about a defined sequence of stuff to it: You can get the element at position n, you can add an element at position n, you can remove the element at position n.In a
Collectionyou can’t do that: “the 5th element in this collection” isn’t defined, because there is no defined order.There are other specialized Collections as well, for example a
Setwhich adds the feature that it will never contain the same element twice.