What is the Difference between Array and Collections (any collection class)? You have a question in your mind or somewhere in an interview, this question has asked.
From a capability perspective, while both can store references to objects:
- Arrays can store primitives. Collections can not store primitives (although they can store the primitive wrapper classes, such as
Integer
etc) - The array is fixed in size Collection are grow-able in nature.
- Memory point of view array is not good, But the collection is better.
let’s see the top array and collection difference in java.
ARRAYS | COLLECTIONS |
1. Arrays are fixed in size. | 1. Collections are growable in nature i.e based on requirement we can increase or decrease the size. |
2. With respect to memory, arrays are not recommended to use.
| 2. With respect to memory, collections are recommended to use. |
3. Arrays can hold only homogeneous data type elements.
| 3. Collections can hold both homogeneous and heterogeneous. |
4. With respect to performance, arrays are recommended to use.
| 4. With respect to performance, collections are not recommended to use. |
5. There is no underlying data structure for arrays and hence readymade method support is not available.
| 5. Every collection class is implemented based on some standard data structure hence readymade method support is available. |
6. The array can hold both primitive and object types.
| 6. Collections can hold only object types, but not primitive types. |
Arrays – Avoid using them unless you have to
Collections – Use them in preference to arrays
Difference between Array and Collections is a basic interview question for Java and Android developers.
Do comment if you have any doubt and suggestions.