Generally Generic: Writing Methods with Generic Parameters

We saw in earlier sections how generics can simplify Java code and make it resistant to ClassCastException problems. In addition to using generics that are part of the JDK, you can write your own. This is useful when you are working with objects that are all of the same type, but you don't know which type it will be until you instantiate your class. This is ideal for classes that have an associated collection of items or involve a lookup.

Let's write a method that uses generic parameters . Recall how we used the ArrayList class earlier—only when we constructed the ArrayList did ...

Get Wicked Cool Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.