// a nearest neighbor in the set to point p; null if the set is empty public Point2D nearest(Point2D p) { if (p == null) throw new NullPointerException("Point2D cannot be null!"); if (set.isEmpty()) return null; double minDistance = Double.MAX_VALUE; Point2D minPoint = null; for (Point2D point : set) { double distance = p.distanceSquaredTo(point); if (distance < minDistance) { minDistance = distance; minPoint = point; } } return minPoint; }
// is the set empty? public boolean isEmpty() { return set.isEmpty(); }