Why is, in Collection<T>
, the method boolean remove(Object o)
not defined as boolean remove(T o)
?
When I have a Collection<String> list
i'm sure that I don't want to do list.remove(new Date())
for instance.
Edit: already solved: Why aren't Java Collections remove methods generic?
Question 2:
When I have a class:
class Value {
Object getValue() {
// return something;
}
}
and then I want to implement a new cool list:
class CoolList<T extends Value> implements Collection<T> {
// ...
@Override
public boolean remove(Object o) {
try {
T value = (T) o; // I mean the cast here
removeInternal(value);
} catch (ClassCastException e) {
return false;
}
}
}
How can I do a clean cast without producing a warning (unchecked cast) and without using @SuspressWarning, is this even possible?
Edit:
Internally I want the list to be able to recover deleted items and because of that I have internally a List<T> deletedItems
.
So the removeInternal() method appends the item to be deleted to the internal deletedItems list and then deletes it and because of that I need some sort of cast.
Thank you.