CS102 · Annotated SlidesCollections — Java Collection Framework Part 2
Collections · Annotated slides

Java Collection Framework Part 2

Sets (HashSet, TreeSet / SortedSet) and maps (HashMap, TreeMap): how they store data, ordering, and their main methods.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up
Slide 1: Slide 1
Exam weight Low
    Slide 2: The Java Collections Framework
    Exam weight Low
      Slide 3: Objectives
      Exam weight Low
        Slide 4: Slide 4
        Exam weight Low
        1. Set: no duplicates. HashSet: no order. TreeSet: sorted.
        Slide 5: Slide 5
        Exam weight Low
          Slide 6: SortedSet and TreeSet
          Exam weight High
          1. TreeSet implements SortedSet: elements kept in ascending order; first(), last(), headSet, tailSet.
          🎯 How this can come up · 1 question
          MCQ

          Which set keeps its elements in sorted order?

          Slide 7: Slide 7
          Exam weight Low
            Slide 8: The Java Collections Framework
            Exam weight Low
              Slide 9: Slide 9
              Exam weight Low
                Slide 10: Class HashSet
                Exam weight High
                1. HashSet: fast add/contains/remove by hashing, no ordering, duplicates ignored.
                🎯 How this can come up · 1 question
                MCQ

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

                Slide 11: Hash Set Demonstration
                Exam weight Low
                  Slide 12: Slide 12
                  Exam weight Low
                    Slide 13: Slide 13
                    Exam weight Low
                      Slide 14: import java.util.*;
                      Exam weight Low
                        Slide 15: Class HashMap
                        Exam weight High
                        1. HashMap: key → value lookups; put, get, containsKey, remove, keySet; keys unique.
                        🎯 How this can come up · 1 question
                        MCQ

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

                        Slide 16: Slide 16
                        Exam weight Low
                          Slide 17: Hash Map Demonstration
                          Exam weight Low
                            Slide 18: Slide 18
                            Exam weight Low
                              Slide 19: TreeMap
                              Exam weight High
                              1. TreeMap: like HashMap but keys are kept sorted; HashMap has no order.
                              Memory trickHash = fast, no order. Tree = sorted.
                              Slide 20: Slide 20
                              Exam weight Low
                                Slide 21: Slide 21
                                Exam weight Low
                                  Slide 22: Summary
                                  Exam weight Low