Junior — Middle
Explain what a collection data structure is and how it is used in programming.
sobes.tech AI
Answer from AI
A collection is a data structure that stores a set of elements and provides methods for adding, removing, and iterating over them. In programming, collections are used for convenient management of groups of objects.
Examples of collections:
- Lists — ordered sets of elements.
- Sets — unique elements without order.
- Maps — key-value pairs.
In Android and Java, collections are widely used for storing data, such as a list of users, a set of unique IDs, or a display of settings.
Example in Kotlin:
val users = listOf("Alice", "Bob", "Charlie")
for (user in users) {
println(user)
}