Skip to main content

Practice ยท 2 of 3

Generic max() with a Bound

Implement static <T extends Comparable<T>> T max(List<T> xs) โ€” returns the largest element per natural ordering. Empty or null list throws IllegalArgumentException. Then two call sites: - static Integer maxInt(List<Integer> xs) delegating to max - static String maxString(List<String> xs) delegating to max This is the bound doing real work: compareTo would not compile without T extends Comparable<T>.

Difficulty: intermediate

Back to lesson: Practice: Generics Lab