Collections
Collection
A group of related data items, such as a list of numbers, a set of names, or a dictionary of key-value pairs.
In programming, collections have multiple defining characteristics:
- Homogeneity :
- Collections typically store similar elements.
- For example, a collection might contain only integers, strings, or objects of a specific class.
- Dynamic size :
- Unlike arrays, collections can grow or shrink as needed.
- This makes them flexible for applications where the number of elements is not known in advance.
- A list of student names in a classroom management system allows teachers to add or remove students as needed.
- This flexibility is essential for managing dynamic data.
Algorithms with Collection Methods
Collection methods are predefined operations that allow you to:
- Add
- Remove
- Access
- Manipulate elements within a collection
These methods are essential for managing data efficiently in programming.
- Collection methods provide a standardised way to interact with data structures.
- They simplify complex operations, making algorithms more readable and maintainable.
Common Collection Methods
- addItem(item): Adds an item to the collection.