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.
- remove(item): Removes a specific item from the collection.
- get(index): Retrieves an item at a specific index.
- contains(item): Checks if an item exists in the collection.
- size(): Returns the number of items in the collection.
- isEmpty(): Checks if the collection is empty.
These methods are often part of abstract data types like lists, sets, and queues.
IB pseudocode has an abstract collection data type that has the following methods:
| Method | Description | Example |
|---|---|---|
| addItem() | Add elemeny | BASKET.addItem() |
| getNext() | Get the next item (does not remove item) | BASKET.getNext() |
| resetNext() | Go back to the first element | BASKET.resetNext() |
| hasNext() | Check if there is next item | BASKET.hasNext() |
| isEmpty() | Check if it is empty | BASKET.isEmpty() |
// Given collection BASKET consisting of strings
TEXT = "I will buy: " // output all items in the basket ITEM = BASKET.getNext() // get first item
TEXT = TEXT + ITEM // add first string
loop while ITEM.hasNext()
ITEM = ITEM.getNext()
TEXT = TEXT + ", " + ITEM
end loop
output TEXTReal-World Examples
In the following scenarios, we will explore Python lists as our collection.
Managing a To-Do list
tasks = [] # initialisation
# adding elements
tasks.append("Buy groceries")
tasks.append("Call mom")
print(tasks)
# removing elements
tasks.remove("Call mom")
print(tasks)
# check if the element is in the list
if "Buy groceries" in tasks: # in other languages usally tasks.contains(element)
print("Task found")Student attendance system
# here Student is seperate class
present_students = [] # initialise
for student in students: # iterate through all students in the class
if student.is_present(): # ambigious function of Student class
present_students.append(student)
print(present_students)Library book management
# initialise
available_books = ["1984", "451 Fahrenheit"]
requested_books = ["1984", "Sherlock Holmes"]
checked_out_books = []
# iterate
for book in requested_books:
if book in available_books:
available_books.remove(book)
checked_out_books.append(book)
# output
print(checked_out_books)- What are the key characteristics of a collection?
- Can you provide an example of a real-world application that uses collections?