Understanding Generics in the Collections Framework
As pointed out, the default classes in the Collections framework were designed to manage only references to objects of type Object
. Polymorphism then permitted the use of any subclass in an implementation of any of these classes. The problem with this approach is that it is not type-safe. Look at this code fragment:
int numberOfApples = 9; String orange = "Valencia"; List stuff = new ArrayList(); stuff.add(numberOfApples); stuff.add(orange); System.out.printf("Stuff: %s%n", stuff);
This code begins by declaring two variables...