0
\$\begingroup\$

I have a simple static class that it's purpose is given an RDD of Point to find the median of each dimension and return that as a new Point using Spark's reduce functions.

This is the class:

public class MedianPointFinder {
 public static Point findMedianPoint(JavaRDD<Point> points) {
 Point biggestPointByXDimension = points.reduce((a, b) -> getBiggestPointByXDimension(a, b));
 Point biggestPointByYDimension = points.reduce((a, b) -> getBiggestPointByYDimension(a, b));
 double xDimensionMedian = biggestPointByXDimension.getX() / 2.0;
 double yDimensionMedian = biggestPointByYDimension.getY() / 2.0;
 return new Point(xDimensionMedian, yDimensionMedian);
 }
 private static Point getBiggestPointByXDimension(Point first, Point second) {
 return first.getX() > second.getX() ? first : second;
 }
 private static Point getBiggestPointByYDimension(Point first, Point second) {
 return first.getY() > second.getY() ? first : second;
 }
}

Point class is a simple class for storing an (x, y) point.

200_success
146k22 gold badges190 silver badges479 bronze badges
asked Nov 25, 2014 at 13:02
\$\endgroup\$
1
  • \$\begingroup\$ My understanding of median is the middle element out of N elements if the N elements were sorted. You are finding the highest element and dividing its value by 2. Is that what you really meant to do? For example, given three x coordinates 1 2 9 I consider the median to be 2 not 4.5. \$\endgroup\$ Commented Nov 28, 2014 at 6:08

1 Answer 1

3
\$\begingroup\$

I guess your Point is not a java.awt.Point since that holds x and y as int and you're using double.

Why not getting rid of your Point class by making the "Finder" class itself a MedianPoint?

import java.awt.geom.Point2D;
public class MedianPoint extends Point2D.Double {
 public MedianPoint(JavaRDD<Point> points) {
 super(
 /* your x calculation */,
 /* your y calculation */
 );
 }
 private static Point2D.Double greaterByXOf(Point2D.Double p1, Point2D.Double p2) {
 // ...
 }
 private static Point2D.Double greaterByYOf(Point2D.Double p1, Point2D.Double p2) {
 // ...
 }
} // MedianPoint
answered Nov 26, 2014 at 1:29
\$\endgroup\$
3
  • \$\begingroup\$ I do need my Point class for some methods that I have created in it that does not exist in Point2D but I guess what I could do is wrap Point2D with my Point class and then do MedianPoint extends Point. \$\endgroup\$ Commented Nov 26, 2014 at 9:15
  • \$\begingroup\$ Are there other reasons why you have to stick to Point? Which make moving Point's methods into MedianPoint impossible? \$\endgroup\$ Commented Nov 26, 2014 at 10:47
  • \$\begingroup\$ No, not really. I just have it for providing a limited API and adding some functionality. I have posted my revised Point class here codereview.stackexchange.com/questions/70902/… \$\endgroup\$ Commented Nov 26, 2014 at 11:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.