O'Reilly logo

Java Generics and Collections by Philip Wadler, Maurice Naftalin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Effective Generics

This chapter contains advice on how to use generics effectively in practical coding. We consider checked collections, security issues, specialized classes, and binary compatibility. The title of this section is an homage to Joshua Bloch’s book, Effective Java (Addison-Wesley).

Take Care when Calling Legacy Code

As we have seen, generic types are checked at compile time, not run time. Usually, this is just what we want, since checking at compile time reports errors earlier and incurs no runtime overhead. However, sometimes this may not be appropriate, either because we can’t be sure that compile-time checks are adequate (say, because we are passing an instance of a parameterized type to a legacy client or to a client we don’t trust), or because we need information about types at run time (say, because we want a reifiable type for use as an array component). A checked collection will often do the trick, and when that will not do, we can create a specialized class. We consider checked collections in this section, security issues in the next section, and specialized classes in the section after that.

Consider a legacy library that contains methods to add items to a given list and to return a new list containing given items:

class LegacyLibrary {
  public static void addItems(List list) {
    list.add(new Integer(1)); list.add("two");
  }
  public static List getItems() {
    List list = new ArrayList();
    list.add(new Integer(3)); list.add("four");
    return list;
  }
}

Now consider ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required