I have the following code snippet:
Set<Short> map=new HashSet<>();
short i=1;
map.add(i);
map.remove(i-1)
It ignores removal because I-1 was automatically casted to integer. But, why there is no remove(V) method with generic? It's bad, I should see an exception or this method should be generic. Doesn't make any sense. Why was it designed this way?
The question is:
If we have Set, why don't we have method remove(E e) inside it? I found that it's because internally there is an array of Object. But why? If we have Set of E generic type, why don't we have E[] as internal storage? For me it's absolutely doesn't make sense.
For example I expected:
class Set<E>{
private E[] values;
public void add(E e);
public void remove(E e);
}
But I got:
class Set<E>{
private Object[] values;
public void add(E e);
public void remove(Object e);
}
Why not to do this?
class Set<E>{
private E[] values;
public void add(E e);
public void remove(E e){
for(E v:values){
if(v.equals(e)){
//remove element
}
}
}
}
????