Skip to content

Commit

Permalink
SPARK-6993 : Add default min, max methods for JavaDoubleRDD
Browse files Browse the repository at this point in the history
The default method will use Guava's Ordering instead of
java.util.Comparator.naturalOrder() because it's not available
in Java 7, only in Java 8.

Author: Olivier Girardot <o.girardot@lateral-thoughts.com>

Closes apache#5571 from ogirardot/master and squashes the following commits:

7fe2e9e [Olivier Girardot] SPARK-6993 : Add default min, max methods for JavaDoubleRDD
  • Loading branch information
Olivier Girardot authored and rxin committed Apr 19, 2015
1 parent 729885e commit 8fbd45c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/src/main/scala/org/apache/spark/api/java/JavaDoubleRDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ class JavaDoubleRDD(val srdd: RDD[scala.Double])
/** Add up the elements in this RDD. */
def sum(): JDouble = srdd.sum()

/**
* Returns the minimum element from this RDD as defined by
* the default comparator natural order.
* @return the minimum of the RDD
*/
def min(): JDouble = min(com.google.common.collect.Ordering.natural())

/**
* Returns the maximum element from this RDD as defined by
* the default comparator natural order.
* @return the maximum of the RDD
*/
def max(): JDouble = max(com.google.common.collect.Ordering.natural())

/**
* Return a [[org.apache.spark.util.StatCounter]] object that captures the mean, variance and
* count of the RDD's elements in one operation.
Expand Down
14 changes: 14 additions & 0 deletions core/src/test/java/org/apache/spark/JavaAPISuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,20 @@ public void min() {
Assert.assertEquals(1.0, max, 0.001);
}

@Test
public void naturalMax() {
JavaDoubleRDD rdd = sc.parallelizeDoubles(Arrays.asList(1.0, 2.0, 3.0, 4.0));
double max = rdd.max();
Assert.assertTrue(4.0 == max);
}

@Test
public void naturalMin() {
JavaDoubleRDD rdd = sc.parallelizeDoubles(Arrays.asList(1.0, 2.0, 3.0, 4.0));
double max = rdd.min();
Assert.assertTrue(1.0 == max);
}

@Test
public void takeOrdered() {
JavaDoubleRDD rdd = sc.parallelizeDoubles(Arrays.asList(1.0, 2.0, 3.0, 4.0));
Expand Down

0 comments on commit 8fbd45c

Please sign in to comment.