Java, from nothing · chapter 7 of 33
The three collections you need: List, Map, Set
Chapter 1.7 · Part 1, Java from nothing · about 35 minutes
What you need before this chapter: chapters 1.1 through 1.6. You should be comfortable with classes, objects, fields, methods, constructors, private, references versus values, == versus equals, and null. You should also know how inheritance and polymorphism work, and how interfaces and abstract classes differ.
When you finish this chapter you will be able to:
- Choose between
List,Map, andSetfor a given piece of data, and say why - Read and write
List<Ticket>,Map<String, Integer>, andSet<String>and know what each stores - Iterate a collection with a for-each loop, and explain why removing from it mid-loop crashes
- Say, from memory, why
ArrayListandHashMapare not safe to share between threads
1. The idea in plain words
A parking lot issues tickets all day. Every ticket needs to sit somewhere while the program runs: a place to add new ones, look old ones up, and go through all of them at closing time. An array can hold a fixed number of tickets, but you rarely know in advance how many cars will show up, and an array cannot grow once you have created it. That gap is what the three types in this chapter fill.
A List is an ordered, growable sequence. It remembers the order you added things in, and it allows duplicates: two identical tickets are two entries, not one. Reach for a List when the answer to "which one" is a position, and when the same value might legitimately appear more than once.
A Map stores a value under a key, so you can retrieve it later without scanning through everything. A parking lot that needs to answer "which spot is plate KA-01-4432 in?" instantly, no matter how many cars are parked, needs a Map from plate to spot number. Reach for a Map whenever you find yourself about to loop through a list just to find one thing by name.
A Set stores values with no duplicates and, usually, no promise about order. A parking lot that wants to know how many distinct plates have been on site today needs a Set. Add every plate as cars arrive, and the size of the set is the answer, duplicates already removed for you.
All three are interfaces, in the same sense chapter 1.5 covered. List, Map, and Set say what a type can do, and you almost always work through the interface type while creating a concrete implementation on the right-hand side. The two implementations you will use for nearly everything in this course are ArrayList for List and HashMap for Map.
Know one fact about both from the first time you touch them: neither ArrayList nor HashMap is safe to use from more than one thread at once. Two threads adding to the same ArrayList at the same time can corrupt its internal state, or silently lose one of the additions. Part 4 of this course deals with what to do about that. For now, in single-threaded code, both are exactly what you want: fast, simple, and the default choice unless something tells you otherwise.
2. Type this
Make a new file, ParkingLot.java, in the folder you have been working in. You will also need the Ticket class from earlier chapters; if you no longer have it, this version is enough to run the example: