Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 7
    Why add() can take a typed parameter but remove() can't is still a bit beyond my comprehension. Josh Bloch would be the definitive reference for Collections questions. It might be all I get without trying to make a similar collection implementation and see for myself. :( Thanks. Commented Sep 22, 2008 at 17:34
  • 2
    Chris - read the Java Generics Tutorial PDF, it will explain why. Commented Jan 27, 2009 at 13:01
  • 46
    Actually, it's very simple! If add() took a wrong object, it would break the collection. It would contain things it's not supposed to! That is not the case for remove(), or contains(). Commented Nov 7, 2009 at 3:46
  • 13
    Incidentally, that basic rule -- using type parameters to prevent actual damage to the collection only -- is followed absolutely consistently in the whole library. Commented Nov 7, 2009 at 3:49
  • 3
    @KevinBourrillion: I've been working with generics for years (in both Java and C#) without ever realising that "actual damage" rule even exists... but now that I've seen it stated directly it makes 100% perfect sense. Thanks for the fish!!! Except now I feel compelled to go back and look at my implementations, to see if some methods can and therefore should be degenerified. Sigh. Commented Aug 22, 2013 at 23:41