June 2004
Intermediate to advanced
848 pages
21h 28m
English
Say we want to write a method that will shuffle the elements in a Collection. We can shuffle a list regardless of what type the elements are. We don't want to have to write one shuffle method that works on List<Integer> and another for List<String> and another for List<Byte>. We need to give shuffle() a parameter that is a “List of anything” and have it move elements around. The usual way to express “List of anything” is “List of Object”, which in the new world of generics is written “List <Object>”
However, there's a big problem with passing around a nice, type-safe, homogeneous “List <String>” and letting someone untrustworthy, like our shuffle() method, get their hands on it as a “List <Object>” A parameter ...
Read now
Unlock full access