Skip to main content

Practice · 3 of 3

Erasure Workaround: the Class Token

CHALLENGE
Difficulty: intermediate+25 XP

Because generics erase, a method that must *create* an instance of T needs runtime type information. Implement: - static <T> List<T> fill(int n, Class<T> type, Function<Object, T> maker): builds a List with n elements, each produced by maker, typed by the token (use java.util.function.Function). - static <T> List<T> nCopiesOf(int n, T value) — erasure-friendly copying that needs no token at all. The tests show which operations survive erasure and which need help.

Back to lesson: Practice: Generics Lab