考虑你有这个列表:
private final List<? extends AbstractXmlElement> inMemoryElements;
它包含一堆 AbstractXmlElement 子类的对象,并且您想要添加一个方法来从该列表中过滤特定子类的对象。为此,我创建了以下方法:
public <E extends AbstractXmlElement> List<E> getInstancesOf(Class<E> c) {
return getUsableElements().stream()
.filter(c::isInstance)
.map(e -> (E) e)
.collect(Collectors.toList());
}
然而 (E) e 导致 UncheckedCast 警告。我想知道这究竟是一个未经检查的强制转换,以及抑制这个警告是否安全,因为那些不是 E 实例的对象在强制转换之前被过滤掉了。这意味着,据我所知,演员阵容永远不应该失败