2

Possible Duplicate:
Why aren't Java Collections remove methods generic?

Does anyone know why MyDamnedUnclearList.remove() methods take Object element and not T element (or V extends T element) ?

I lost two hours to find a bug mixing both int and Integer Java types (in a List<Thread> list...

Community
  • 1
  • 1
Adrien M.
  • 313
  • 1
  • 3
  • 9
  • Also this one http://stackoverflow.com/questions/857420/what-are-the-reasons-why-map-getobject-key-is-not-fully-generic Which I included in my answer – Adam Gent Apr 17 '12 at 22:21
  • Hmm the question now talks about Threads... weird edit. – Adam Gent Apr 17 '12 at 22:23
  • Thread was just my actual situation, just a example. I just wanted to show it was not generic, as you mentioned in your clear answer. – Adrien M. Apr 17 '12 at 22:27

1 Answers1

1

The issue has to do with backward compatibility of existing code and specification.

What are the reasons why Map.get(Object key) is not (fully) generic

Its all in the details of the Javadoc:

boolean remove(Object o)

Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).

So you see it doesn't matter whether the objects have the same type only that they equal.

Community
  • 1
  • 1
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • I've read this javadoc, I just consider it was nonsense, IHMO. – Adrien M. Apr 17 '12 at 22:29
  • Java is a very old language that has gone through many iterations and they try like hell to be backward compatible. This is akin to Python < 3.0. Java unfortunately does not have a benevolent dictator like Ruby and Python so changes that break stuff are very hard to get placed in the language (we have an evil fat/lazy dictator named oracle). If it really bothers you I recommend you look at Scala (which has the opposite problem of changing everything every release). – Adam Gent Apr 18 '12 at 00:44
  • I often use and love coding in Scala or Haskell :) – Adrien M. Apr 18 '12 at 14:59