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

Java Collection Framework

The Collections Framework (interfaces, implementations, algorithms), Collection vs. Map, Set vs. List, ArrayList and its methods, iterators, the diamond <>, Collections.sort and Collections.binarySearch.

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: Slide 2
    Exam weight Low
      Slide 3: Slide 3
      Exam weight Low
      1. Java Collections Framework: interfaces + classes that implement common data structures and algorithms (java.util).
      Slide 4: Collections Framework is a unified architecture for managing collections
      Exam weight Low
      1. Three parts: interfaces, implementations (classes), algorithms (static methods in Collections).
      Slide 5: Slide 5
      Exam weight High
      1. Core interfaces: Collection, Set (no duplicates), List (ordered, duplicates allowed), Queue, Map (key → value).
      🎯 How this can come up · 1 question
      MCQ

      Which collection type does NOT allow duplicate elements?

      Slide 6: Slide 6
      Exam weight Low
        Slide 7: Collection
        Exam weight Low
          Slide 8: Map
          Exam weight High
          1. Map is NOT a Collection: it stores key–value pairs.
          🎯 How this can come up · 1 question
          True / False

          Map extends the Collection interface.

          Slide 9: Slide 9
          Exam weight Low
            Slide 10: Collections Methods
            Exam weight Low
              Slide 11: Slide 11
              Exam weight Low
                Slide 12: Slide 12
                Exam weight Low
                  Slide 13: — A List (sometimes called a sequence ) is an
                  Exam weight Low
                  1. List: ordered, duplicates allowed, zero-based indexes (ArrayList, LinkedList).
                  Slide 14: Slide 14
                  Exam weight Low
                    Slide 15: Array List
                    Exam weight High
                    1. ArrayList: like an array but resizes automatically; holds objects only (Integer, not int).
                    🎯 How this can come up · 1 question
                    MCQ

                    An ArrayList<int> declaration:

                    Slide 16: Introduction to Collections and Class ArrayList
                    Exam weight Low
                      Slide 17: Introduction to Collections and Class ArrayList (Cont.)
                      Exam weight Low
                        Slide 18: Slide 18
                        Exam weight Low
                          Slide 19: Introduction to Collections and Class ArrayList (Cont.)
                          Exam weight High
                          1. Methods: add, add(index, e), get, set, remove, size, contains, indexOf, clear, isEmpty.
                          🎯 How this can come up · 1 question
                          MCQ

                          Which returns the number of elements in an ArrayList list?

                          Slide 20: Slide 20
                          Exam weight Low
                            Slide 21: Slide 21
                            Exam weight Low
                              Slide 22: Slide 22
                              Exam weight Low
                                Slide 23: Slide 23
                                Exam weight Low
                                  Slide 24: and ArrayList Iterator
                                  Exam weight High
                                  1. Iterator: hasNext(), next(), remove(): the safe way to remove while iterating.
                                  🎯 How this can come up · 1 question
                                  True / False

                                  Removing elements from a list inside a for-each loop is safe.

                                  Slide 25: Collection Interface
                                  Exam weight Low
                                    Slide 26: Slide 26
                                    Exam weight Low
                                      Slide 27: and ArrayList Iterator
                                      Exam weight Low
                                      1. Java 7 diamond: List<String> list = new ArrayList<>();
                                      Slide 28: Method sort
                                      Exam weight Low
                                      1. Collections.sort(list) sorts a List (natural order or with a Comparator).
                                      Slide 29: Slide 29
                                      Exam weight Low
                                        Slide 30: Collection Method Sort
                                        Exam weight Low
                                          Slide 31: Slide 31
                                          Exam weight Low
                                            Slide 32: Binary Search
                                            Exam weight High
                                            1. Collections.binarySearch(sortedList, key) returns the index, or −(insertion point) − 1 if absent. The list must be sorted first.
                                            🎯 How this can come up · 1 question
                                            MCQ

                                            Collections.binarySearch requires the list to be:

                                            Slide 33: public class GFG { public static void main(String[] args) {
                                            Exam weight Low