【问题标题】:Find closest entries in a list by comparing with one entry通过与一个条目进行比较来查找列表中最接近的条目
【发布时间】:2019-08-26 22:23:22
【问题描述】:

我有一个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

}

我现在有一个Unit 的列表,它是一个包含许多单元的List 对象。现在我需要找到从List 对象到Unit 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;
  }

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

【问题讨论】:

  • 1) 创建一个Map,其中单位为键,到单位 x 的距离为值。 2) 使用this question 中的一种解决方案按值对地图进行排序。

标签: java algorithm sorting data-structures


【解决方案1】:

你说你知道如何计算距离,所以我下面的代码不包括计算,所以我假设你可以实现calculateDistance() 方法。我使用TreeMap 自动对添加到其中的条目进行排序,而Double 类实现Comparable,因此您无需处理排序。 Iterator 将返回按计算距离排序的键。

private List<Unit> nearestUnits(List<Unit> lists, Unit x, int limit) {
    TreeMap<Double, Unit> sorted = new TreeMap<>();
    List<Unit> output = new ArrayList<>();
    for (Unit unit : lists) {
        Double distance = calculateDistance(unit, x);
        sorted.put(distance, unit);
    }
    Set<Double> keys = sorted.keySet();
    Iterator<Double> iter = keys.iterator();
    int count = 0;
    while (iter.hasNext() && count < limit) {
        Double key = iter.next();
        Unit val = sorted.get(key);
        output.add(val);
        count++;
    }
    return output;
}

【讨论】:

  • 这更不可读,仅供参考TreeMap::putO(log n)(这不是HashMap),所以插入n 元素将是O(n log n)ArrayList::sort 也是 O(n log n) 可读性要好得多。
  • @Jai,很酷,所以请提供您自己的答案并启发我们。
【解决方案2】:

//这个距离方法参考来自https://stackoverflow.com/a/16794680/6138660

public static double distance(double lat1, double lat2, double lon1,
        double lon2) {
    final int R = 6371; // Radius of the earth

    double latDistance = Math.toRadians(lat2 - lat1);
    double lonDistance = Math.toRadians(lon2 - lon1);
    double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
            + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
            * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = R * c * 1000; // convert to meters

    distance = Math.pow(distance, 2);

    return Math.sqrt(distance);
}

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


    lists.sort(new Comparator<Unit>() {

        @Override
        public int compare(Unit o1, Unit o2) {

            double flagLat = x.getLat();
            double flagLon = x.getLon();

            double o1DistanceFromFlag = distance(flagLat, o1.getLat(), flagLon, o1.getLon());
            double o2DistanceFromFlag = distance(flagLat, o2.getLat(), flagLon, o2.getLon());

            return Double.compare(o1DistanceFromFlag, o2DistanceFromFlag);
        }
    });

    return lists.subList(0, limit);;
  }

【讨论】:

  • 这可行,但创建一个 UnitDistance { Unit unit; double distance; } 列表然后按距离排序会更快:每个单位仅计算一次距离其他选项:填写 aa TreeMap&lt;Double,Unit&gt; .
猜你喜欢
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2017-06-25
相关资源
最近更新 更多