📚

Java Generics & Collections Overview

Jun 11, 2025

Overview

This lecture covers Java generics and collections, focusing on key object-oriented programming (OOP) concepts, the use and limitations of ArrayList, custom generic classes, collection interfaces, and the Iterator pattern.

Object-Oriented Programming (OOP) Concepts

  • Encapsulation hides internal state using private properties and behaviors.
  • Realization denotes implementing interfaces.
  • Composition represents "has-a" relationships (fields).
  • Inheritance represents "is-a" relationships using base and derived classes.
  • Polymorphism refers to method overriding (not overloading).
  • Java has primitive/reference types, subtypes, and static/dynamic types.

Collections and ArrayLists

  • Collections are Java's predefined data structures for storing homogeneous objects.
  • ArrayList<T> automatically grows to accommodate more elements.
  • Key ArrayList methods: add, clear, indexOf, get, remove, set, size, toString.
  • Enhanced for-loop can be used to traverse ArrayLists.
  • Growing an ArrayList requires copying elements, so adding to the end is efficient; adding elsewhere is expensive.

Generics in Java

  • Generic classes/interfaces use type variables as parameters (e.g., ArrayList<E>).
  • Generic type variables are usually uppercase letters (e.g., T, K, V).
  • Generics make classes and methods more flexible and type-safe.
  • All generic types must be reference types, not primitives; use wrapper classes (e.g., Integer for int).

Defining and Using Generic Classes/Methods

  • Example: Pair<K, V> stores a key and value of generic types K and V.
  • Records can be used for concise immutable data classes.
  • Diamond operator < > allows type inference by the compiler.
  • Generic methods can declare type variables for method-specific generics (e.g., swapping Pair types).

Limitations and Wildcards in Generics

  • Cannot instantiate generic type variables or create arrays with them.
  • Cannot use generic type parameters in static contexts.
  • Wildcard types (e.g., List<? extends Number>) allow collections of any subclass of a type.
  • List<Integer> is not a subtype of List<Number>; use wildcards to increase flexibility.

Java Collection Framework

  • Main interfaces: Collection<E>, Set<E>, List<E>, Queue<E>, Map<K,V>.
  • Collections group objects; Maps connect keys to values without duplicate keys.
  • Use interfaces (e.g., Collection) before implementations (e.g., ArrayList) for flexibility.
  • Example: MyMap<K, V> using Collection internally instead of ArrayList.

The Iterator Pattern

  • Iterator allows traversing a collection without exposing its internal structure.
  • Interface Iterator<E> methods: hasNext(), next(), and remove().
  • All collections provide a method to create an Iterator.
  • Iterable<E> interface provides the iterator() method.
  • Iterator tracks position and supports safe traversal/modification.

Key Terms & Definitions

  • Encapsulation — Hiding internal data using private members.
  • Generic Type — Placeholder for a type parameter in a class or method.
  • ArrayList — A resizable array implementation in Java.
  • Wrapper Class — Object representation for primitive types (e.g., Integer for int).
  • Diamond Operator — <>; infers generic types automatically.
  • Iterator — Object for traversing collections safely.
  • Wildcard Type — ?; allows flexibility in assigning collection types.
  • Record — Concise, immutable data class in Java.

Action Items / Next Steps

  • Practice using ArrayList and generics in sample Java programs.
  • Read about Java's collection hierarchy and the Iterator pattern.
  • Prepare for Lecture 6: Collections continued.