利用foreachActive的在Java火花矢量 [英] Use of foreachActive for spark Vector in Java

查看:1532
本文介绍了利用foreachActive的在Java火花矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用Java编写简单的code这迭代稀疏矢量活性元素?

How to write simple code in Java which iterate over active elements in sparse vector?

假设我们有这样的矢量:

Lets say we have such Vector:

Vector sv = Vectors.sparse(3, new int[] {0, 2}, new double[] {1.0, 3.0});

我是用拉姆达或功能2尝试(来自三个不同的进口,但总是失败)。如果您使用的功能2,请提供必要的进口。

I was trying with lambda or Function2 (from three different imports but always failed). If you use Function2 please provide necessary import.

推荐答案

阿德里安,这里是你如何使用上稀疏的 foreachActive 方法矢量

Adrian, here is how you can use the foreachActive method on the sparse Vector

    AbstractFunction2<Object, Object, BoxedUnit> f = new AbstractFunction2<Object, Object, BoxedUnit>() {
        public BoxedUnit apply(Object t1, Object t2) {
            System.out.println("Index:" + t1 + "      Value:" + t2);
            return BoxedUnit.UNIT;
        }
    };

    Vector sv = Vectors.sparse(3, new int[]{0, 2}, new double[]{1.0, 3.0});
    sv.foreachActive(f);

这将通过稀疏向量和输出:

This will go through the sparse vector and output:

Index:0      Value:1.0
Index:2      Value:3.0

有4进口:

import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.mllib.linalg.Vectors;
import scala.runtime.AbstractFunction2;
import scala.runtime.BoxedUnit;

这篇关于利用foreachActive的在Java火花矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆