// Little program to exercise our generic Stack public static void main(String[] args) {
Stack stack = new Stack<>();
for (String arg : args)
stack.push(arg);
while (!stack.isEmpty())
System.out.println(
stack.pop().toUpperCase() );
}
CHAPTER 5 GENERICS 134
The foregoing example may appear to contradict Item 28, which encourages
the use of lists in preference to arrays. It is not always possible or desirable to use
lists inside your generic types. Java doesn’t support lists natively, so some generic
types, such as
ArrayList
,
must be implemented atop arrays. Other generic types,
such as
HashMap
, are implemented atop arrays for performance.
The great majority of generic types are like our
Stack
example in that their
type parameters have no restrictions: you can create a
Stack