what java generics gets wrong about hashes

If you’re actually using a hash, this is what you really want to do:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@SuppressWarnings({ "unchecked", "serial" })
public class DrSuess {

   public static void main(String[] args)
   {
      Map foo = new HashMap()
      {
         {
            put(1, "fish");
            put(2, "fish");
            put("red", "fish");
            put("blue", "fish");
         }
      };

      Iterator i = foo.keySet().iterator();
      while(i.hasNext())
      {
         Object key = i.next();
         System.out.println(key + " " + foo.get(key));
      }
   }
}

but generics frowns on it.  A Dictionary could be  <String, String> or an AssociativeArray could be <String, Object>  but a Hash wants to be able to put anything in it.  Give me useful subclasses, not the useless crap that is Java generics.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s