我有一个 B 类对象的 ArrayList,所有对象都实现了 Serialized 接口
public void copy(List<B> listToCopy){
List<B> list = listToCopy.strem().map(SerializationUtils::clone).collect(Collectors.toList());
}
编译错误表明列表属性需要一个列表而不是一个列表 在我看到的所有示例中(甚至在 StackOverflow 论坛中),它们都没有使用任何类型的 Cast 那么我如何使用 Apache Commons Lanf 库解决这个问题?
提前致谢
我使用以下类作为 B 类:
public class Test implements Serializable {
public Test(String string) {
this.string = string;
}
private String string;
}
当我运行以下代码时,我无法重现它:
public void test() {
List<Test> listToCopy = List.of(new Test("a"), new Test( "b"));
List<Test> list = listToCopy.stream().map(SerializationUtils::clone).collect(Collectors.toList());
}
我在以下版本中使用
SerializationUtils
类:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
您能否验证您是否使用的是最新版本的 commons-lang3?如果仍然存在问题,请分享更多信息(例如:完整的堆栈跟踪或可运行的示例)?