Graphs { public static <T> List<T> findClosestObjects(Point pos, Collection<T> objects, Function<T, Point> transformation, int n) { checkArgument(n > 0, "n must be positive."); final List<ObjectWithDistance<T>> objs = new ArrayList<>(); for (final T obj : objects) { @Nullable final Point objPos = transformation.apply(obj); checkNotNull(objPos); objs.add(new ObjectWithDistance<>(obj, Point.distance(pos, objPos))); } Collections.sort(objs); final List<T> results = new ArrayList<>(); for (final ObjectWithDistance<T> o : objs.subList(0, Math.min(n, objs.size()))) { results.add(o.obj); } return results; } private  Graphs(); static void addPath(Graph<E> graph,
      Point... path); static void addPath(Graph<E> graph,
      Iterable<Point> path); static void addBiPath(Graph<E> graph,
      Point... path); static void addBiPath(Graph<E> graph,
      Iterable<Point> path); static Graph<E> unmodifiableGraph(
      Graph<E> graph); static boolean equal(Graph<?> g1, @Nullable Object other); static List<Point> shortestPathEuclideanDistance(
      Graph<E> graph, final Point from, final Point to); static List<Point> shortestPath(
      Graph<E> graph, final Point from, final Point to, GeomHeuristic h); @Nullable static T findClosestObject(Point pos, Collection<T> objects,
      Function<T, Point> transformation); static List<T> findClosestObjects(Point pos,
      Collection<T> objects, Function<T, Point> transformation, int n); static double pathLength(List<Point> path); static ImmutableList<Point> getExtremes(Graph<?> graph);  }
@Test public void closestObjectsTest() { final Function<Point, Point> f = new Function<Point, Point>() { @Override public Point apply(Point input) { return input; } }; final List<Point> points = Arrays.asList(new Point(10, 34), new Point(234, 2), new Point(10, 10), new Point(1, 1)); final List<Point> results = Graphs.findClosestObjects(new Point(0, 0), points, f, 2); assertEquals(results.size(), 2); assertEquals(new Point(1, 1), results.get(0)); assertEquals(new Point(10, 10), results.get(1)); final List<Point> results2 = Graphs.findClosestObjects(new Point(0, 0), points, f, 5); assertEquals(results2.size(), 4); assertEquals(new Point(1, 1), results2.get(0)); assertEquals(new Point(10, 10), results2.get(1)); assertEquals(new Point(10, 34), results2.get(2)); assertEquals(new Point(234, 2), results2.get(3)); }