Level 1 · Apprentice0 XP
Write code once for many types, and let the compiler check it.
Step 1 / 5
ProblemBefore generics a list held Object. You put a String in, someone else put an Integer in, and the cast on the way out threw ClassCastException in production. The compiler could not help: it did not know what the list was for.
← → step
Before this: Files, I/O and resources
🛠️ Your turn
Reading is not the skill, doing is. Try for ten minutes before you open the hint. Getting stuck first is what makes the solution stick.
1.Write a generic class Pair<A, B> with a method swap() returning Pair<B, A>, and a static method <T> T firstOr(List<T> list, T fallback). Then write sumAll(List<? extends Number>) and call it with a List<Integer> and a List<Double>.