Skip to content

Commit 8fbd45c

Browse files
Olivier Girardotrxin
authored andcommitted
SPARK-6993 : Add default min, max methods for JavaDoubleRDD
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 <[email protected]> Closes apache#5571 from ogirardot/master and squashes the following commits: 7fe2e9e [Olivier Girardot] SPARK-6993 : Add default min, max methods for JavaDoubleRDD
1 parent 729885e commit 8fbd45c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

core/src/main/scala/org/apache/spark/api/java/JavaDoubleRDD.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ class JavaDoubleRDD(val srdd: RDD[scala.Double])
163163
/** Add up the elements in this RDD. */
164164
def sum(): JDouble = srdd.sum()
165165

166+
/**
167+
* Returns the minimum element from this RDD as defined by
168+
* the default comparator natural order.
169+
* @return the minimum of the RDD
170+
*/
171+
def min(): JDouble = min(com.google.common.collect.Ordering.natural())
172+
173+
/**
174+
* Returns the maximum element from this RDD as defined by
175+
* the default comparator natural order.
176+
* @return the maximum of the RDD
177+
*/
178+
def max(): JDouble = max(com.google.common.collect.Ordering.natural())
179+
166180
/**
167181
* Return a [[org.apache.spark.util.StatCounter]] object that captures the mean, variance and
168182
* count of the RDD's elements in one operation.

core/src/test/java/org/apache/spark/JavaAPISuite.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,20 @@ public void min() {
761761
Assert.assertEquals(1.0, max, 0.001);
762762
}
763763

764+
@Test
765+
public void naturalMax() {
766+
JavaDoubleRDD rdd = sc.parallelizeDoubles(Arrays.asList(1.0, 2.0, 3.0, 4.0));
767+
double max = rdd.max();
768+
Assert.assertTrue(4.0 == max);
769+
}
770+
771+
@Test
772+
public void naturalMin() {
773+
JavaDoubleRDD rdd = sc.parallelizeDoubles(Arrays.asList(1.0, 2.0, 3.0, 4.0));
774+
double max = rdd.min();
775+
Assert.assertTrue(1.0 == max);
776+
}
777+
764778
@Test
765779
public void takeOrdered() {
766780
JavaDoubleRDD rdd = sc.parallelizeDoubles(Arrays.asList(1.0, 2.0, 3.0, 4.0));

0 commit comments

Comments
 (0)