GCGrowCarrer

Generics

Locked

Lesson 16 of 60 · DataStructures · Medium

Finish these first:Interfaces and Abstract Classes →Or pass the tests below to unlock it now.

1

The big idea

Generics add compile-time type safety: List<String, Map<K,V, bounded types <T extends Comparable<T. Types are erased at runtime.

Think of it like this: Think of data structures as specialized toolboxes: a List keeps items in numbered slots (`0, 1, 2...`), a Set keeps only unique badges, and a Dictionary/Map labels each compartment with a custom name key.
Key words (tap to learn):
2

Read the lesson

Generics add compile-time type safety: List<String>, Map<K,V>, bounded types <T extends Comparable<T>>. Types are erased at runtime.

static <T extends Comparable<T>> T max(T a, T b) {
    return a.compareTo(b) >= 0 ? a : b;
}

PECS: Producer Extends, Consumer Super for wildcards.

3

Quick check

Before writing code: What should `Solution.max(3,9)` return when your implementation of Generics is working?

4

Your challenge

2 sample tests, 1 hidden

Write generic static <T extends Comparable<T>> T max(T a, T b).

Stuck? Get a hint (free)
More learning tools(compare languages, visual guide, related lessons)

Compare with another language

See how Java compares side-by-side with Python.

Visual walkthrough

A diagram, a step-by-step animation, and short captions for Generics.

Mastery0%

Learn this first

Opens up next