Generics A Brief Review Generics n n A

Generics A Brief Review

Generics n n A generic is a method that is recompiled with different types as the need arises The bad news: n n n The good news: n n n Instead of saying: List words = new Array. List(); You'll have to say: List<String> words = new Array. List<String>(); Replaces runtime type checks with compile-time checks No casting; instead of String title = (String) words. get(i); you use String title = words. get(i); Some classes and interfaces that have been “genericized” are: Vector, Array. List, Linked. List, Hashtable, Hash. Map, Stack, Queue, Priority. Queue, Dictionary, Tree. Map and Tree. Set

Writing generic methods n n private void print. List. Of. Strings(List<String> list) { for (Iterator<String> i = list. iterator(); i. has. Next(); ) { System. out. println(i. next()); } } This method should be called with a parameter of type List<String>, but it can be called with a parameter of type List n n n The disadvantage is that the compiler won’t catch errors; instead, errors will cause a Class. Cast. Exception This is necessary for backward compatibility Similarly, the Iterator need not be an Iterator<String>

Type wildcards n Here’s a simple (no generics) method to print out any list: n n The above still works in Java 1. 5, but now it generates warning messages n n private void print. List(List list) { for (Iterator i = list. iterator(); i. has. Next(); ) { System. out. println(i. next()); } } Java 1. 5 incorporates lint (like C lint) to look for possible problems You should eliminate all errors and warnings in your final code, so you need to tell Java that any type is acceptable: n private void print. List. Of. Strings(List<? > list) { for (Iterator<? > i = list. iterator(); i. has. Next(); ) { System. out. println(i. next()); } }

Writing your own generic types n public class Box<T> { private List<T> contents; public Box() { contents = new Array. List<T>(); } public void add(T thing) { contents. add(thing); } public T grab() { if (contents. size() > 0) return contents. remove(0); else return null; n } Sun recommends single capital letters (such as T) for type variables

Generic Iterators n To iterate over generic collections, it’s a good idea to use a generic iterator n n n List<String> list. Of. Strings = new Linked. List<String>(); . . . for (Iterator<String> i = list. Of. Strings. iterator(); i. has. Next(); ) { String s = i. next(); System. out. println(s); } Or use the new for statement: for(type var : array) {. . . } or for(type var : collection) {. . . } You can use the new for statement with your own generic types, if they implement the Iterable interface n This requires an Iterator<T> iterator() method

Summary n If you think of a genericized type as a type, you won’t go far wrong n n n Use it wherever a type would be used Array. List my. List becomes Array. List<String> my. List new Array. List() becomes new Array. List<String>() public Array. List reverse(Array. List list) becomes public Array. List<String> reverse(Array. List<String> list) Advantage: Instead of having collections of “Objects”, you can control the type of object Disadvantage: more complex, more typing

The End
- Slides: 8