January 18, 2005

Collection shortcuts

Arrays.asList(“Rod”, “Jane”, “Freddy”); - Dion points out the shortcut to creating a List. One of the libraries I always have handy does this for Maps and Sets too, so you can do:

Map m = StructUtil.map( new String[] { "foo", "bar", "baz", "blargle" } );

Not as nice as the Rhino or Groovy (or Perl!) versions, but still handy. (Java's lack of shortcuts for stuff like this is really frustrating, although the varargs in 1.5 should make this simpler.) If you implement this yourself I recommend using LinkedHashMap as an implementation so you get the keys back in the same order you inserted them.

Next: Norton Antivirus message caused by moving shortcuts
Previous: How not to create an issue management system