The Artima Developer Community
Sponsored Link

Dr. Dichotomy's Development Diary
Java Generics: Don't Repeat Yourself
by Eamonn McManus
November 10, 2004
Summary
A minor annoyance with Java generics is that you often end up repeating type parameters in a variable declaration and in the constructor invocation that initializes that variable. Defining a trivial static method allows you to avoid this.

Advertisement

Cédric Beust, in an interesting review of new Java language features, notes something that most people who have used Generics will have seen. You very often end up declaring variables something like this:

Map<String, List<Account>> accounts =
  new HashMap<String, List<Account>>();

The repetition is annoying and error-prone. Cédric writes:

Not only is the code significantly harder to read, but it fails to obey the DRY principle ("Don't repeat yourself"). What if I need to change the value type of this map from List<Account> to Collection<Account>? I need to replace all these statements everywhere in my code. While IDE refactoring will help, it's still an awful lot of code for a modification of this kind that hardly impacts the semantics of this code.

Fortunately, there is a simple way to define Util.newMap() such that you can write:

Map<String, List<Account>> accounts = Util.newMap();

The definition is simply:

public static <K,V> Map<K,V> newMap() {
    return new HashMap<K,V>();
}

An additional advantage is that you don't have to write the ugly concrete type HashMap in your code.

The compiler can figure out what kind of Map you want if you assign to a Map variable, though not if you pass Util.newMap() as a parameter to a method. Then you might need a temporary variable.

Obviously you can do the same thing for other generic types like List and Set.

I've logged this as a Request For Enhancement on java.util.Collections.

Talk Back!

Have an opinion? Readers have already posted 12 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Eamonn McManus adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Eamonn McManus is the technical lead of the JMX team at Sun Microsystems. As such he heads the technical work on JSR 3 (JMX API) and JSR 160 (JMX Remote API). In a previous life, he worked at the Open Software Foundation's Research Institute on the Mach microkernel and countless other things, including a TCP/IP stack written in Java. In an even previouser life, he worked on modem firmware in Z80 assembler. He is Irish, but lives and works in France and in French. His first name is pronounced Aymun (more or less) and is correctly written with an acute accent on the first letter, which however he long ago despaired of getting intact through computer systems.

This weblog entry is Copyright © 2004 Eamonn McManus. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us