Why does Java's TreeSet remove(Object) not take an E

前端 未结 4 740
刺人心
刺人心 2020-12-16 23:27

From the Java 6 TreeSet Documentation:

boolean remove(Object o):
    Removes the specified element from this set if it is present.

4条回答
  •  遥遥无期
    2020-12-17 00:08

    remove(), like get() is required to work when given an equal element (in terms of .equals()). In Java, it is possible (and in some cases, required) for objects of different classes to be equal. Hence, you shouldn't restrict the type.

提交回复
热议问题