A a = new A();
ArrayList<A> myA = (ArrayList<A>)request.getAttribute("message");
错误消息:
Type safety: Unchecked cast from
Object
toArrayList<A>
属性“消息”在 ArrayList<A>
中发送来自另一个jsp页面。
最佳答案
如果您想取消该警告,我会执行如下操作:
List<A> castToArrayList() {
Object obj = request.getAttribute("message");
return ((List<?>) obj).stream().map(el -> (A) el).collect(Collectors.toList());
or
return ((List<?>)obj)
.stream()
.filter(A.class::isInstance)
.map(A.class::cast)
.collect(Collectors.toList());
}
关于java - 从 Object 转换为 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41778276/