I have a list of ScheduleContainer
objects and in the stream each element should be casted to type ScheduleIntervalContainer
. Is there a way of doing t
It's possible, but you should first consider if you need casting at all or just the function should operate on subclass type from the very beginning.
Downcasting requires special care and you should first check if given object can be casted down by:
object instanceof ScheduleIntervalContainer
Then you can cast it nicely by:
(ScheduleIntervalContainer) object
So, the whole flow should look like:
collection.stream()
.filter(obj -> obj instanceof ScheduleIntervalContainer)
.map(obj -> (ScheduleIntervalContainer) obj)
// other operations