java的类型转换让人忍不住在开始编码前骂一声 FUCK!
1 子类转父类 up
List<SubClass> subs = ...; List<? extends BaseClass> bases = subs;
2 父类转子类 down
//强转
public class TestA {} public class TestB extends TestA{} List<TestB> variable = (List<TestB>)(List<?>) collectionOfListA;
3 stream方式
collection.stream() .filter(obj -> obj instanceof ScheduleIntervalContainer) .map(obj -> (ScheduleIntervalContainer) obj)
参考:
2 https://stackoverflow.com/questions/35743525/java-8-stream-cast-list-items-to-type-of-subclass