通过与一个条目进行比较来查找列表中最近的条目

我有一个类,里面有很多字段,如下所示:Unit


public class Unit {

  private final int id;

  private final int beds;

  private final String city;

  private final double lat;

  private final double lon;


  // constructors and getters here

  // toString method


}

我现在有一个列表,其中是一个包含许多单位的对象。现在我需要找到从对象到 最近的单位。通过限制限制结果。UnitListListUnit x


  private List<Unit> nearestUnits(List<Unit> lists, Unit x, int limit) {

    List<Unit> output = new ArrayList<>();


    // how do I sort lists object in such a way so that I can get nearest units here to "x"?


    return output;

  }

我们在类中存在纬度/经度,因此我们可以使用它来计算欧氏距离并进行比较。我对如何按最短距离对单位列表进行排序并获取最近的单位感到困惑。到目前为止,我正在使用 Java 7,所以我不能使用 Java 8。Unit


慕沐林林
浏览 168回答 2
2回答

临摹微笑

你说你知道如何计算距离,所以我下面的代码不包括计算,所以我假设你可以实现这个方法。我使用自动对添加到其中的条目和类实现进行排序,因此您无需处理排序。将返回按计算的距离排序的键。calculateDistance()TreeMapDoubleComparableIteratorprivate List<Unit> nearestUnits(List<Unit> lists, Unit x, int limit) {&nbsp; &nbsp; TreeMap<Double, Unit> sorted = new TreeMap<>();&nbsp; &nbsp; List<Unit> output = new ArrayList<>();&nbsp; &nbsp; for (Unit unit : lists) {&nbsp; &nbsp; &nbsp; &nbsp; Double distance = calculateDistance(unit, x);&nbsp; &nbsp; &nbsp; &nbsp; sorted.put(distance, unit);&nbsp; &nbsp; }&nbsp; &nbsp; Set<Double> keys = sorted.keySet();&nbsp; &nbsp; Iterator<Double> iter = keys.iterator();&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; while (iter.hasNext() && count < limit) {&nbsp; &nbsp; &nbsp; &nbsp; Double key = iter.next();&nbsp; &nbsp; &nbsp; &nbsp; Unit val = sorted.get(key);&nbsp; &nbsp; &nbsp; &nbsp; output.add(val);&nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; }&nbsp; &nbsp; return output;}

PIPIONE

此距离方法参考自&nbsp;https://stackoverflow.com/a/16794680/6138660public static double distance(double lat1, double lat2, double lon1,&nbsp; &nbsp; &nbsp; &nbsp; double lon2) {&nbsp; &nbsp; final int R = 6371; // Radius of the earth&nbsp; &nbsp; double latDistance = Math.toRadians(lat2 - lat1);&nbsp; &nbsp; double lonDistance = Math.toRadians(lon2 - lon1);&nbsp; &nbsp; double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);&nbsp; &nbsp; double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));&nbsp; &nbsp; double distance = R * c * 1000; // convert to meters&nbsp; &nbsp; distance = Math.pow(distance, 2);&nbsp; &nbsp; return Math.sqrt(distance);}private List<Unit> nearestUnits(List<Unit> lists, Unit x, int limit) {&nbsp; &nbsp; lists.sort(new Comparator<Unit>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public int compare(Unit o1, Unit o2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double flagLat = x.getLat();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double flagLon = x.getLon();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double o1DistanceFromFlag = distance(flagLat, o1.getLat(), flagLon, o1.getLon());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double o2DistanceFromFlag = distance(flagLat, o2.getLat(), flagLon, o2.getLon());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Double.compare(o1DistanceFromFlag, o2DistanceFromFlag);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; return lists.subList(0, limit);;&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java