Jan 24
Easier Java Generics object construction
General No Comments »I’ve been programming with JDK 5.0 for a bit now and just love the generics features. However, I find that I keep repeating myself when constructing collection instances.
For example:
Map<String, Collection<File>> files =
new HashMap<String, Collection<File>>();
I recently discovered in this blog posting that a simple static method would allow:
Map<String, Collection<File>> files = Util.newMap();
The code for the utility (excerpted from the blog entry referenced above) looks like this:
public static <K,V> Map<K,V> newMap()
{
return new HashMap<K,V>();
}
Clearly, saves a bunch of typing and is easy to implement. The author of the posting mentioned he’s added it as an RFE as well.
