6 Replies Latest reply: Oct 8, 2010 7:42 PM by EJP RSS

    java.util.Map<K,V> interface, why is it not entirely Generic?

    843793
      I was looking at the API for the java.util.Map interface and I noticed that a lot of its methods aren't entirely Generic. I was was wondering why that is. These methods are:
      boolean containsKey(Object)
      boolean containsValue(Object)
      V get(Object)
      V remove(Object)
      Is there any particular reason why it's better than making all the methods Generic? like a performance reason?

      If it's for backwards compatibility, I thought that Generics automatically defaults to the class that it extends from if it's running on an older Java (for example, Collection<E> defaulting to Collection, or <N extends Number> N defaulting to Number.)

      For specific details on what I was doing, I was trying to make a java.util.Map that's also a java.util.Collection just for the sake of an exercise. And I was having trouble with the Map's V remove(Object key)
      method, which makes the Map interface incompatible with Collection's boolean remove(Object o).

      To be fair, java.util.Collection<E> isn't entirely Generic, but I am asking myself now if this is something that I should consider implementing in Generic classes as well, or if it's better to have a completely Generic class (when needed, of course) like I originally thought.

      My general question is, why make it only partially Generic instead of going the full way with Generics or leaving it un-generic.