CS102 · Collections · Annotated SlidesJava Collection Framework Part 2
Original
Overview

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. 1 Slide 1
  2. 2 The Java Collections Framework
  3. 3 Objectives
  4. 4 Sets
  5. 5 Sets (cont.)
  6. 6 SortedSet and TreeSet
  7. 7 Slide 7
  8. 8 The Java Collections Framework
  9. 9 Slide 9
  10. 10 Class HashSet
  11. 11 Hash Set Demonstration
  12. 12 HashSet hashCode() method
  13. 13 Slide 13
  14. 14 import java.util.*;
  15. 15 Class HashMap
  16. 16 Slide 16
  17. 17 Hash Map Demonstration
  18. 18 Slide 18
  19. 19 TreeMap
  20. 20 Slide 20
  21. 21 We have inserted the data in random order however
  22. 22 Summary
Slide 1 / 22

Slide 1

Slide 1: Slide 1
Slide 2 / 22

The Java Collections Framework

Slide 2: The Java Collections Framework
Slide 3 / 22

Objectives

Slide 3: Objectives
Slide 4 / 22

Sets

Slide 4: Sets
Slide 5 / 22

Sets (cont.)

Slide 5: Sets (cont.)
Slide 6 / 22

SortedSet and TreeSet

Slide 6: SortedSet and TreeSet

Notes

  1. TreeSet implements SortedSet: elements kept in ascending order; first(), last(), headSet, tailSet.

Exam angle

PredictionQ1

Which set keeps its elements in sorted order?

Slide 7 / 22

Slide 7

Slide 7: Slide 7
Slide 8 / 22

The Java Collections Framework

Slide 8: The Java Collections Framework
Slide 9 / 22

Slide 9

Slide 9: Slide 9
Slide 10 / 22

Class HashSet

Slide 10: Class HashSet

Notes

  1. HashSet: fast add/contains/remove by hashing, no ordering, duplicates ignored.

Exam angle

PredictionQ1

Adding "a" twice to a HashSet<String> gives a size of:

Slide 11 / 22

Hash Set Demonstration

Slide 11: Hash Set Demonstration
Slide 12 / 22

HashSet hashCode() method

Slide 12: HashSet hashCode() method
Slide 13 / 22

Slide 13

Slide 13: Slide 13
Slide 14 / 22

import java.util.*;

Slide 14: import java.util.*;
Slide 15 / 22

Class HashMap

Slide 15: Class HashMap

Notes

  1. HashMap: key → value lookups; put, get, containsKey, remove, keySet; keys unique.

Exam angle

PredictionQ1

map.put("x", 1); map.put("x", 2); map.get("x") returns:

Slide 16 / 22

Slide 16

Slide 16: Slide 16
Slide 17 / 22

Hash Map Demonstration

Slide 17: Hash Map Demonstration
Slide 18 / 22

Slide 18

Slide 18: Slide 18
Slide 19 / 22

TreeMap

Slide 19: TreeMap

Notes

  1. TreeMap: like HashMap but keys are kept sorted; HashMap has no order.

  2. Memory trick Hash = fast, no order. Tree = sorted.

Slide 20 / 22

Slide 20

Slide 20: Slide 20
Slide 21 / 22

We have inserted the data in random order however

Slide 21: We have inserted the data in random order however
Slide 22 / 22

Summary

Slide 22: Summary