如何将 apache.spark.ml.linalg.Vector 转换为 java 中的 arrayList?

How to convert apache.spark.ml.linalg.Vector to arrayList in java?

我正在尝试将 apache.spark.ml.linalg.Vector 转换为 Java 中的 ArrayList

源码是这样的:

Vector vector = (Vector) row.get(1);
ArrayList<String> vectorList = new ArrayList<String>(vector);

错误是:

Cannot resolve constructor
'ArrayList(org.apache.spark.ml.linalg.Vector)'

我该怎么做?

您不能将 Vector 直接转换为 ArrayList<String>Vector 提供了一个 toArray() 方法 (documentation),它 returns 一个 double 类型的数组,使用它你可以将它转换成一个 ArrayList<String> 作为下面:

        Vector vector = (Vector) row.get(1);
        double[] vectorArray = vector.toArray();
        ArrayList<String> vectorList = new ArrayList<>();
        for (double vectorElement: vectorArray) {
            vectorList.add(String.valueOf(vectorElement));
        }