Card 07/ 08
All 8 cards
ExerciseDifficulty: Advanced1 min
Five Requirements: Name the Collection for Each
Five short requirements. Name the collection for each and say what the requirement rules out. All five before the reveal.
| The requirement | |
|---|---|
| 1 | Record which customer references have been seen, and ask often |
| 2 | The same, but the report must list them in the order they arrived |
| 3 | The same, but the report must list them alphabetically |
| 4 | Hold work items and always take the most urgent one next |
| 5 | Undo history: the last action taken is the first one undone |
The five answers, and what each requirement rules out
| Use | What the requirement ruled out | |
|---|---|---|
| 1 | HashSet | A list, because contains on one is a walk |
| 2 | LinkedHashSet | HashSet, whose iteration order is not promised |
| 3 | TreeSet | Both hash sets — or sort a copy at the end, if adding is hot |
| 4 | PriorityQueue | ArrayDeque, which has no opinion about urgency |
| 5 | ArrayDeque used as a stack | Stack, which is synchronised and exposes Vector |
The third has two defensible answers, which is the point of including it. A TreeSet keeps order on every add; sorting a HashSet once at the end does the work only once. Which wins depends on how many adds there are per report.