Bob
Bob

Reputation: 10795

Spark example Java 7 to Java 8

I am reading Apache Spark examples, which are written in Java 7. For example, this code:

public final class JavaKMeans {

  private static class ParsePoint implements Function<String, Vector> {
    private static final Pattern SPACE = Pattern.compile(" ");

    @Override
    public Vector call(String line) {
      String[] tok = SPACE.split(line);
      double[] point = new double[tok.length];
      for (int i = 0; i < tok.length; ++i) {
        point[i] = Double.parseDouble(tok[i]);
      }
      return Vectors.dense(point);
    }
  }

  ...
  JavaRDD<Vector> points = lines.map(new ParsePoint());
  ...
}

How this call method can be rewritten using Java 8?

Upvotes: 2

Views: 894

Answers (1)

assylias
assylias

Reputation: 328598

You could refactor it slightly like below (not tested):

public final class JavaKMeans {
  private static final Pattern SPACE = Pattern.compile(" ");

  private Vector call(String line) {
    double[] point = SPACE.splitAsStream(line)
                        .mapToDouble(Double::parseDouble)
                        .toArray();
    return Vectors.dense(point);
  }

  ...
  JavaRDD<Vector> points = lines.map(this::call);
  ...
}

Upvotes: 4

Related Questions