Skip to main content

Reusable Bounded Collector API

Challenge on lesson: Checkpoint: Generics & PECS

Build a small generic API inside Solution: - static <T extends Comparable<T>> List<T> topN(List<T> xs, int n) — the n largest elements in DESCENDING order; n larger than size returns all; n <= 0 throws IllegalArgumentException; xs null/empty throws too. - static double sumNumbers(List<? extends Number> xs) — PECS producer. - static <T> List<? super T> drain(List<T> src, List<? super T> dst) — moves every element from src into dst and returns dst; src ends empty. All three signatures must keep the generic promises above (the tests compile call sites that would fail with weaker types).

Difficulty: intermediate

Back to lesson: Checkpoint: Generics & PECS