That’s very general

Java Generics and Collections by M Naftalin and P Wadler

This is very much a book of two halves.

The first part of the book is a tutorial on Java Generics, and includes coverage of wild cards, subtyping, bounds, reification and reflection.  Coming from the CLR world, the implementation of Java Generics by using erasure sounds very strange, and the book does a good job of explaining the benefits of this implementation technique. It discusses how erasure allows generic and non-generic code and libraries to interact, with the system inserting casts at the appropriate points with the knowledge that they cannot fail at runtime. Unfortunately there are edge cases, where the casts can fail and the book goes into some discussion about why this is. In these cases the compiler will emit a warning, though this is at compile time, so it isn’t always useful if you are using a library with such problems, as often you are provided with the compiled bytecode and never see these warnings, but the inserted casts can fail in interesting ways at runtime making it hard to deduce the actual root cause.

There’s loads of good material and comments in the first half, not all of it related to generics. For example, the book points out that implementing the compare method on integer values as

   int CompareTo(int x, int y) { return x – y; }

is wrong, because in unchecked arithmetic negating int.MINVALUE equals int.MINVALUE.

The second part of the book covers the Java Collections. Generics have made it easy to define a rich set of collection types and the book recommends using them instead of the more traditional array types. There’s a good discussion of whether arrays should be considered a deprecated type because of the way that they interact with generics and in particular because they should only be used for reifiable types.

The Java Collection library is quite impressive, containing a large number of datatypes, with some being concurrently accessible. The types include HashSets and LinkedHashSets, Sorted Sets, NavigableSets, TreeSets, Queues including PriorityQueues and ConcurrentLinkedQueues, Lists, Maps including HashMaps, and WeakHashMaps, SortedMaps and NavigableMaps.

Advertisements
This entry was posted in Books. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s