O'Reilly logo
  • Shayne Riley thinks this is interesting:

Item 29: Consider typesafe heterogeneous containers


Cover of Effective Java, 2nd Edition


For example, a database row can have arbitrarily many columns, and it would be nice to be able to access all of them in a typesafe manner. Luckily, there is an easy way to achieve this effect. The idea is to parameterize the key instead of the container. Then present the parameterized key to the container to insert or retrieve a value. The generic type system is used to guarantee that the type of the value agrees with its key.