Java Reference
In-Depth Information
SingleLinkQueue<String> queue =
new SingleLinkQueue<String>();
queue.add("Hello");
queue.add("World");
Now there is no need for a cast when invoking
remove
:
String hello = queue.remove();
Nor is it possible to add the wrong kind of element to the queue:
queue.add(25); // INVALID: won't compile
Generic types are invaluable in defining these kind of collection classes,
as you'll see in
Chapter 21
,
but they have uses in many other situations.