Collections.
We have learned about combining values into records. But what if you have many values of the same type? Not three products. Thirty. Or three thousand.
Today we learn about collections. Data structures that hold multiple values. Lists. Sets. Maps. The workhorses of data management.
Programs often work with groups of things.
A list of users. A set of tags. A catalog of products. A mapping of IDs to names.
You cannot create a record with a thousand fields for a thousand products. You need a structure that holds an arbitrary number of values.
Collections solve this. A collection is a container for multiple values. The number of values can vary. Zero, ten, a million — collections scale.
Collections are fundamental. Almost every program uses them. Understanding collections is essential to programming.
A list is an ordered sequence of elements.
Elements have positions. The first element, the second element, the last element. Order matters and is preserved.
Lists can contain duplicates. The same value can appear multiple times.
Consider: apple, banana, cherry, apple. This list has four elements. Apple appears twice. That is allowed.
Common operations on lists: add an element, remove an element, get element at position, find if element exists, get the length.
Lists are versatile. Task queues. Search results. Message histories. Any time you have an ordered sequence of things.
A set is an unordered collection of unique elements.
No duplicates. Each element appears at most once. If you add an element that already exists, nothing changes.
Consider: apple, banana, cherry as a set. Order is not guaranteed. You cannot ask for "the first element". You can only ask "is this element in the set?"
Why use sets? When uniqueness matters. Tags on an article. Users who have voted. Permissions a user has. You care what is present, not the order.
Common operations: add element, remove element, check membership, union of two sets, intersection of two sets.
Sets are efficient for membership checks. "Is this user in the admin set?" This is fast with sets.
A map associates keys with values. It is also called a dictionary or associative array.
Each key maps to exactly one value. Keys must be unique. Values can repeat.
Consider: Alice maps to 30, Bob maps to 25, Charlie maps to 35. This map associates names with ages. Given a name (the key), you can look up the age (the value).
Maps are for lookups. Given this, what is that? User ID to user data. Country code to country name. Word to definition.
Common operations: put a key-value pair, get value by key, remove by key, check if key exists.
Maps are efficient for lookups. Finding a value by key is fast, regardless of how many entries exist.
How do you choose which collection to use?
Use a list when order matters or duplicates are allowed. A sequence of events. A queue of tasks. Search results in ranked order.
Use a set when you need uniqueness and do not care about order. Tags. Permissions. Items already processed.
Use a map when you need to look up values by keys. Configuration settings. Caches. Any key-value relationship.
Often the choice is clear. Sometimes multiple collections could work. Choose the one that best matches how you will use the data.
The operations you need guide the choice. Need to look up by key? Map. Need to ensure uniqueness? Set. Need ordered access? List.
Collections share common operations.
Size: how many elements does it contain?
Empty check: is the collection empty?
Contains: is this element in the collection?
Iteration: go through each element one by one.
These work on all collection types. The specific operations differ, but these are universal.
Other operations are type-specific. Lists have indexing. Sets have union and intersection. Maps have key lookup.
Learning these operations gives you a vocabulary for working with data. Most data manipulation is combining these operations.
A common task is processing each element in a collection.
You iterate: go through elements one by one and do something with each.
For each user in users, print user dot name.
Iteration works with all collection types. Lists iterate in order. Sets iterate in some order (but do not rely on it). Maps iterate over key-value pairs.
Iteration is how you transform data. Filter out unwanted elements. Transform elements to a new form. Accumulate a result.
Most data processing is iteration plus operations on each element.
Collections can contain collections.
A list of lists: a matrix or grid. A map of lists: groups of items by category. A set of sets: groups that must be unique.
Consider categories as a map where "fruit" maps to a list containing apple and banana, and "vegetable" maps to a list containing carrot and broccoli.
This is a map where values are lists. Given a category, get a list of items.
Nesting allows modeling complex data. But too much nesting becomes hard to understand. Often, creating named types for the inner structures helps clarity.
Collections can be empty. Zero elements.
An empty list. An empty set. An empty map.
Empty collections are valid and important. A user with no orders. A search with no results. Handle these cases.
When working with collections, always consider: what if it is empty? Does your code handle that correctly?
Also consider: what if it has one element? What if it has millions? Good code works for all sizes.
Let us summarize.
Collections hold multiple values of the same type.
Lists are ordered sequences that allow duplicates. Use for sequences and queues.
Sets are unordered unique collections. Use when uniqueness matters.
Maps associate keys with values. Use for lookups.
Common operations: size, contains, iteration.
Collections can be nested for complex structures.
Always handle empty collections properly.
Collections are fundamental. Master them and you can handle almost any data.