Skip to main content

Practice ยท 3 of 3

Erasure Workaround: the Class Token

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.

Difficulty: intermediate

Back to lesson: Practice: Generics Lab