Why aren't Java Collections remove methods generic?

前端 未结 10 2152
旧巷少年郎
旧巷少年郎 2020-11-22 04:49

Why isn\'t Collection.remove(Object o) generic?

Seems like Collection could have boolean remove(E o);

Then, when you ac

相关标签:
10条回答
  • 2020-11-22 05:33

    Another reason is because of interfaces. Here is an example to show it :

    public interface A {}
    
    public interface B {}
    
    public class MyClass implements A, B {}
    
    public static void main(String[] args) {
       Collection<A> collection = new ArrayList<>();
       MyClass item = new MyClass();
       collection.add(item);  // works fine
       B b = item; // valid
       collection.remove(b); /* It works because the remove method accepts an Object. If it was generic, this would not work */
    }
    
    0 讨论(0)
  • 2020-11-22 05:41

    I always figured this was because remove() has no reason to care what type of object you give it. It's easy enough, regardless, to check if that object is one of the ones the Collection contains, since it can call equals() on anything. It's necessary to check type on add() to ensure that it only contains objects of that type.

    0 讨论(0)
  • 2020-11-22 05:43

    Because if your type parameter is a wildcard, you can't use a generic remove method.

    I seem to recall running into this question with Map's get(Object) method. The get method in this case isn't generic, though it should reasonably expect to be passed an object of the same type as the first type parameter. I realized that if you're passing around Maps with a wildcard as the first type parameter, then there's no way to get an element out of the Map with that method, if that argument was generic. Wildcard arguments can't really be satisfied, because the compiler can't guarantee that the type is correct. I speculate that the reason add is generic is that you're expected to guarantee that the type is correct before adding it to the collection. However, when removing an object, if the type is incorrect then it won't match anything anyway. If the argument were a wildcard the method would simply be unusable, even though you may have an object which you can GUARANTEE belongs to that collection, because you just got a reference to it in the previous line....

    I probably didn't explain it very well, but it seems logical enough to me.

    0 讨论(0)
  • 2020-11-22 05:43

    Because it would break existing (pre-Java5) code. e.g.,

    Set stringSet = new HashSet();
    // do some stuff...
    Object o = "foobar";
    stringSet.remove(o);
    

    Now you might say the above code is wrong, but suppose that o came from a heterogeneous set of objects (i.e., it contained strings, number, objects, etc.). You want to remove all the matches, which was legal because remove would just ignore the non-strings because they were non-equal. But if you make it remove(String o), that no longer works.

    0 讨论(0)
  • 2020-11-22 05:46

    Remove is not a generic method so that existing code using a non-generic collection will still compile and still have the same behavior.

    See http://www.ibm.com/developerworks/java/library/j-jtp01255.html for details.

    Edit: A commenter asks why the add method is generic. [...removed my explanation...] Second commenter answered the question from firebird84 much better than me.

    0 讨论(0)
  • 2020-11-22 05:49

    It was a compromise. Both approaches have their advantage:

    • remove(Object o)
      • is more flexible. For example it allows to iterate through a list of numbers and remove them from a list of longs.
      • code that uses this flexibility can be more easily generified
    • remove(E e) brings more type safety to what most programs want to do by detecting subtle bugs at compile time, like mistakenly trying to remove an integer from a list of shorts.

    Backwards compatibility was always a major goal when evolving the Java API, therefore remove(Object o) was chosen because it made generifying existing code easier. If backwards compatibility had NOT been an issue, I'm guessing the designers would have chosen remove(E e).

    0 讨论(0)
提交回复
热议问题