• Contact me

    Your Name (required)

    Your Email (required)

    Subject

    Your Message

  • HealthcareGuy.com
  • |
  • ShahidShah.com
  • |
  • FedSphere.com

Easier Java Generics object construction

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Invite me to speak at your event

    Invite me to speak at your event

  • KLOUT Score

Trustcloud Score