Generics in Java is noisy sometimes. The parameterized types are thrown out by the compiler anyway after compile, so my question is, is there really any drawbacks in ignoring them, as long as I declare the type of the reference to include parameterized types? e.g.,
Map<String, Map<Integer, String>> myMap = new HashMap<String, Map<Integer,String>>();
is just too noisy. What if I write:
@SuppressWarnings("unchecked")
Map<String, Map<Integer, String>> myMap = new HashMap();
It’s so much clearer, except that Eclipse will put a disgusting wavy line under it. Put @SuppressWarning will make Eclipse happy, but many people (including me) don’t like to sprinkle @SuppressWarning in the code. Is it really that bad to ignore the type parameters in “new HashMap()”?
I think that having compiler warnings but ignoring them makes them useless. Having supresswarnings is fine in theory, but it can be abused, so it can be a bad habit. This will be fixed in Java 7, where the syntax will be:
Until then, Google collections does it for you, but it is too simple to do yourself to get the library for that reason: