I am writing a method removeAll for an ArrayList. By the interface, removeAll takes a Collection<?>, however, for whatever reason, I want the Collection to be of only Strings. Is there any way to do this for the entire Collection at once?
(I am not talking about going through each element individually, like this:
for (Object el : c) {
if (!(el instanceof String)) {
throw new ClassCastException(String.format("Object %s is not of type String.", el.toString()));
}
}
That is my backup, but I'd rather check the entire Collection at once if it's possible.)
I did try:
public boolean removeAll(Collection<?> c) {
return subMethod((Collection<? extends String>) c, true); //true is unimportant here
}
but that just casts any Collection<Wrapper of Primitive> to a Collection<String>.
Edit: To be clear, I would like some other way to ensure the Collection only contains Strings.