Characteristics of Specialized Containers
A heterogeneous container can hold elements of different data types.
A Shelf That Accepts Anything
A container is a structure that holds several values together. Once you start working with containers, an important question comes up: are all of the values inside a container required to be of the same kind? The answer separates containers into two families, heterogeneous containers and homogeneous containers. Knowing which family a container belongs to tells you what kinds of values it can store, and that determines how you can use it.
What do you think happens?
Imagine a shelf where every item must be the same kind of object, such as all books. What happens if you try to place a cup on that shelf?
Reveal answer
The cup breaks the rule. A shelf that allows only one kind of item does not accept a cup alongside books.
This shelf is the everyday version of a homogeneous container. It enforces one kind of item. A second shelf that lets you place a book, a cup, and a pencil side by side accepts mixed kinds, which is the everyday version of a heterogeneous container. Programming containers behave in these same two ways, and each behavior has its own name.
The Question to Ask About Any Container
The question to ask about any container is not how many values it holds, but what kinds of values it is permitted to hold together. Two containers can hold the same number of values and still differ completely in what they accept. The number of slots tells you about size. The permitted element types tell you about behavior.
Classify a container by asking one question: does this container allow its elements to differ in type, or does it require them to match?
Heterogeneous Containers
A heterogeneous container can hold elements of different data types. It is not restricted to one kind of value, so its elements may come from different types.
The defining feature of a heterogeneous container is freedom in the element types. This is the property to remember, because it is what sets pair and tuple apart from the other containers in this family of discussion. When you meet a heterogeneous container, you should not expect every element to share a type with its neighbors.
Tracing a Mixed Shelf
Follow a heterogeneous container through three placements. Each position, or slot, can hold a value of a different type, so the contents change in kind as you move along the container.
After the three placements, the container holds a book, a cup, and a pencil. The three values are of different kinds, and the container permits that. This is exactly what the heterogeneous family means. Notice that the rule is about what the container allows, not about what you happened to put in it.
Homogeneous Containers and the Single-Type Rule
A homogeneous container holds elements that all share the same data type. Every element in it must have one type.
The rule is strict but simple: every element in a homogeneous container shares one type. When you read or write code that uses such a container, you can rely on that fact. Every element you pull out has the same type as the others, which is why this family is described as uniform. The single-type rule is a promise the container makes to you, and it is what makes the family predictable.
Classifying Pair, Tuple, and Everything Else
The two families are sorted by the same test. Pair and tuple are heterogeneous, because their elements may differ in type. All other containers in this discussion are homogeneous, meaning their elements all share one type. Pair and tuple are the reference points for the heterogeneous family, so when you meet either one, you should expect mixed element types to be allowed.
Comparing the Two Families Side by Side
The table below sets the two families side by side so you can classify containers quickly. Read each row as a question about what the container allows.
| Property | Heterogeneous container | Homogeneous container |
|---|---|---|
| Element types | May differ from one element to another | All share one data type |
| Defining rule | Freedom in the element types | Single-type rule |
| Examples given in the source | Pair and tuple | All other containers |
| What you can rely on | Elements may be of different kinds | Every element has the same type |
The two families differ on one property: whether element types may differ.
Mistakes About Mixed Types
Common mistake Assuming every container can mix element types
What it looks likeA learner reads that a container holds several values and concludes that any container can hold a book, a cup, and a pencil together.
Why it failsOnly the heterogeneous family permits elements of different types. A homogeneous container follows the single-type rule, so its elements all share one type. Holding several values does not mean holding different kinds of values.
Do this insteadBefore relying on mixed types, classify the container by asking whether its elements may differ in type. Pair and tuple are heterogeneous, and all other containers in this discussion are homogeneous.
Common mistake Judging a container by its size instead of its permitted types
What it looks likeTwo containers each hold three values, so a learner treats them as interchangeable.
Why it failsThe number of values says nothing about which kinds of values are allowed together. The question that matters is what kinds of values the container may hold together.
Do this insteadAsk what element types the container permits, not how many values it holds.
Classifying a Container on Sight
A container is described as holding a book, a cup, and a pencil, and its elements may come from different types. Which family does it belong to, and what single question did you use to decide?
Hints
- Ask whether the elements are allowed to differ in type.
- Mixed element types point to the heterogeneous family.
- Pair and tuple are the source's heterogeneous examples.
A container is described as holding only books, and every element you pull out is known to be a book. Classify it, then explain why a cup could not be added without breaking its defining rule.
Hints
- Check whether all elements share one data type.
- The single-type rule is what makes the family uniform.
Key Takeaways
- A heterogeneous container can hold elements of different data types, because its defining feature is freedom in the element types.
- A homogeneous container holds elements that all share the same data type, following a strict single-type rule.
- Pair and tuple are classified as heterogeneous, and all other containers in this discussion are homogeneous.
- Classify any container by asking whether its elements may differ in type, not by how many values it holds.
- Do not assume every container can mix element types, since a homogeneous container rejects a mixed element.