我有这样的数据:
23.3445556 72.4535455 0.23434
23.3645556 72.4235455 0.53434
23.3245556 72.4635455 0.21434
23.3645556 72.2535455 0.25434
我想像这样制作HashMap
:
HashMap<23.34444,72.23455,0.2345566> demo = new HashMap()
这里 23.34444,72.23455
是键,0.2345566
是值。
这是因为我想像这样遍历HashMap
:
if(demo.latitude < 21.45454545 && demo.longitude > 72.3455)
//get the value from hashMap
long lat repn map 上的特定像素,每个像素具有相同的值,我想从特定区域获得平均值假设 x y 和像素将高达 100 万
- 我想知道这是不是一个好方法,因为它每天都会受到数百万的点击
最佳答案
您可以使用 Point 类开始。
https://docs.oracle.com/javase/7/docs/api/java/awt/Point.html
int xE6 = x*1e6
int yE6 = y*1e6
new Point(xE6, yE6)
但由于这是 awt 特定的并且是对类的误用,您可能最终会想要创建自己的类。
public final class LatLon {
private double lat;
private double lon;
public LatLon(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LatLon latLon = (LatLon) o;
if (Double.compare(latLon.lat, lat) != 0) return false;
return Double.compare(latLon.lon, lon) == 0;
}
@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(lat);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(lon);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon;
}
}
(使用 IntelliJ 自动生成)
这可以像这样使用
public static void main(String[] args) {
HashMap<LatLon, Double> demo = new HashMap<LatLon, Double>();
demo.put(new LatLon(23.3445556,72.4535455), 0.23434);
demo.put(new LatLon(23.3645556,72.4235455), 0.53434);
demo.put(new LatLon(23.3245556,72.4635455), 0.21434);
demo.put(new LatLon(23.3645556,72.2535455), 0.25434);
System.out.println(demo.get(new LatLon(23.3645556,72.2535455))); //0.25434
}
按原样使用此类的问题在于它使用了 double 值。您需要某种精度,由小数点位置给出。
double 有奇怪的数学,可能会给你带来准确性错误,所以我衷心推荐使用为地理坐标设计的库。
特别给予
if(demo.latitude<21.45454545 && demo.longitude >72.3455)
如果您最终遇到性能问题,这种检查最好由某种专门构建的集合来处理边界检查和坐标。
关于java - Java中将经纬度作为HashMap的Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44515273/