Which set keeps its elements in sorted order?
Java Collection Framework Part 2
Sets (HashSet, TreeSet / SortedSet) and maps (HashMap, TreeMap): how they store data, ordering, and their main methods.
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 22 slides
- 1 Slide 1
- 2 The Java Collections Framework
- 3 Objectives
- 4 Sets
- 5 Sets (cont.)
- 6 SortedSet and TreeSet
- 7 Slide 7
- 8 The Java Collections Framework
- 9 Slide 9
- 10 Class HashSet
- 11 Hash Set Demonstration
- 12 HashSet hashCode() method
- 13 Slide 13
- 14 import java.util.*;
- 15 Class HashMap
- 16 Slide 16
- 17 Hash Map Demonstration
- 18 Slide 18
- 19 TreeMap
- 20 Slide 20
- 21 We have inserted the data in random order however
- 22 Summary
SortedSet and TreeSet
Notes
TreeSet implements SortedSet: elements kept in ascending order; first(), last(), headSet, tailSet.
Exam angle
Class HashSet
Notes
HashSet: fast add/contains/remove by hashing, no ordering, duplicates ignored.
Exam angle
Adding "a" twice to a HashSet<String> gives a size of:
Class HashMap
Notes
HashMap: key → value lookups; put, get, containsKey, remove, keySet; keys unique.
Exam angle
map.put("x", 1); map.put("x", 2); map.get("x") returns:
- The second put replaces the value.
TreeMap
Notes
TreeMap: like HashMap but keys are kept sorted; HashMap has no order.
Memory trick Hash = fast, no order. Tree = sorted.