OE-3.4 Collections Framework & I/O
The Java collections framework and stream classes — written September 2026
What this is and why it exists
The collections framework is the library you will use every single day of Java work. So learn it the way you will use it: by the shape of the problem.
Three questions pick the class for you. Ordered or not. Duplicates allowed or not. Keyed or not. Nearly every choice falls out of those.
There is one silent defect waiting here, and it catches everyone once. Storing your own class in a hash-based collection only works if you have overridden equality and hashing correctly.
The vocabulary
- Collection — a container of elements.
- List — ordered, duplicates allowed.
- Set — no duplicates.
- Sorted set — no duplicates, kept in order.
- Map — keyed lookup.
- Iterator — the standard way of walking a collection.
- Comparator — an object saying how two elements compare.
- Byte stream — reads and writes raw bytes.
- Character stream — reads and writes text, with an encoding.
The mental model
Start with the three questions. Ordered? Duplicates? Keyed? A list is ordered and allows duplicates. A set refuses duplicates. A sorted set refuses duplicates and keeps order. A map is keyed lookup. Underneath, the choices from the data structures module reappear. One list is an array that grows, another is linked nodes. One set is a hash table, another a search tree. The costs you learned there are the costs here, which means you already know why one iterates fast and another finds fast.
Iterators are the standard way of walking any of them, which is what lets code work across collection types without caring which it was given.
Now the defect. A hash-based collection finds an element by computing a number from it and going to that position. Store your own class without telling Java how to compare two instances and how to hash one, and it falls back to identity. Two objects that ought to be equal are then treated as different. The result is a set containing what look like duplicates, or a map lookup that fails on a key you can see is present. Nothing reports it. Override both, and override them consistently: two objects that are equal must hash the same.
Comparators are how you supply an ordering the class does not have on its own, or a different one from the natural order. Same collection, different arrangement, decided at the call site.
The legacy classes are older equivalents that predate the framework. Know their names, because they turn up in old code, and know that they are not what you would choose today.
On input and output, keep two families apart. Byte streams move raw bytes; character streams move text and involve an encoding. Reading text through a byte stream is the byte-and-character confusion from the Programming area in a new costume. It breaks on the first name with an accent in it.
What you should now be able to explain or do
Pick a collection from three questions about the problem. Connect each collection to the structure underneath it and its costs. Walk any collection with an iterator. Override equality and hashing consistently, and say what breaks when you do not. Supply a comparator for a non-natural ordering. Read and write files, keeping byte and character streams apart, and say what goes wrong when you mix them.
Check yourself
Which three questions pick a collection?
Ordered or not, duplicates allowed or not, keyed or not. Nearly every choice falls out of those three.
Why do the data structures costs carry over?
Because the collections are those structures. One list is a growable array, another linked nodes; one set is a hash table, another a search tree.
What goes wrong storing your own class in a hash-based collection?
Without overriding equality and hashing it falls back to identity, so equal objects look different. Lookups fail silently on keys that are present.
What must stay consistent between equality and hashing?
Two objects that compare equal must produce the same hash. Otherwise the lookup goes to the wrong position.
What separates byte streams from character streams?
Byte streams move raw bytes; character streams move text and involve an encoding. Mixing them breaks on the first accented character.
Go deeper
We haven't checked most of these for screen reader use yet.
Back to Collections Framework & I/O: work through the checklist