Given a class T
which is a subclass of U
, is it safe to cast Iterator<T>
to Iterator<U>
? And assuming that the cast be safe, are there more elegant (and warning-free) ways of doing it other than:
Iterator<T> it = <insert your Iterator<T> here...>;
Iterator<U> it2 = (Iterator<U>)(Iterator<? extends U>)it;
My reasoning why the cast is not dangerous is this:
Iterator<?>
does not support inserting elements, so there is no danger of corrupting the underlying data structure by using Iterator<U>
instead of Iterator<T>
;Consumer<? super U>
is convertible to Consumer<? super T>
but not vice versa. Hence, Iterator<U>::forEachRemaining(Consumer<? super U> action)
will accept a subset of all actions eligible for the same method in Iterator<T>
and, in particular, does not introduce a way of executing actions not supported by Iterator<T>
; Iterator<U>::remove()
is equivalent to Iterator<T>::remove()
;Iterator<U>::next()
returns a superclass of T;I agree, I don't see any reason it should be unsafe.
I believe that
Iterator<U> it2 = (Iterator) it;
should be sufficient to make the cast compile, but you would still have to suppress the warnings if you wanted it without a warning. Suppressing a warning is fine if you understand the reason for the warning and know it will not apply in this case.
Perhaps whatever code you are using that demands an Iterator<U>
should be refactored to accept Iterator<? extends U>
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With