如何将 vavr 集合转换为 Java 数组?

How to convert vavr collection to Java array?

我知道 https://www.javadoc.io/doc/io.vavr/vavr/latest/io/vavr/Value.html#toJavaArray 但它已被弃用。是否有可以使用的非弃用方法?

在三个 toJavaArray 变体中,只有 toJavaArray(Class) 变体被直接弃用,其他两个被标记为已弃用,因为 Value class 本身已被弃用.不幸的是,生成的 javadoc 对区分两者不是很有帮助,但您可以检查 source code directly. This picture is probably a more precise representation of the current state:

Value 已弃用,取而代之的是 io.vavr.Iterable. This new interface is an extension of the java.lang.Iterable type, that differs mainly in that it returns an io.vavr.collection.Iterator 而不是简单的 java.util.Iteratorio.vavr.collection.Iterator 扩展了 Value,我希望在删除 Value 接口后,Value 接口中未明确弃用的所有方法都将移至 [=16] =].

在两个未明确弃用的 toJavaArray 方法中,我会使用 T[] toJavaArray(IntFunction<T[]> arrayFactory) 变体——这是我对 vavr 项目的贡献之一——因为它 returns 类型正确类型的数组而不是 Object[].

总而言之,我不会使用 vavrValue.toJavaArray(),而是使用 vavrValue.iterator().toJavaArray(SomeType[]::new)