Java add set removal methods

Why methods add(<T> element)and remove(Object o)taking various arguments?

For example, in Set<Short>you add short elements. Why does the remove method accept Object? If you cannot add any other data type, why are you deleting another data type?

Thank.

+2
source share
2 answers

add(<T> element): to add only item T.

remove(Object o): you can remove the T element even if it refers to an Object link.

For instance:

T t = new T();
Set<Short> set = new HashSet<Short>();
Short number = 2;
set.add(number);
Object numberObject = number;
set.remove(numberObject) // it will remove 2 from the set.

why are you deleting another data type? we do not delete another data type, but we can delete data even if it is referenced by an object reference (as in the example).

+2
source

remove(obj) , (obj == null ? e ==null : obj.equals(e)) true. - equals(Object), Object, obj e .

0

All Articles