我正在尝试将位置列表中的所有唯一坐标添加到单个 HashMap 中,该 HashMap 以坐标为键,计数为值。关键是用'$'符号连接的经度和纬度。
//String = coordinates concatinated with '$', Integer = coordinate occurrence count
private HashMap<String, Integer> coordMap = new HashMap<String, Integer>();
...
public void addCoords(double longitude, double latitude) {
//The total count of the state
stateCount++;
String coordsConcat = longitude + "$" + latitude;
//Here we're removing the entry of the existing coord, then re-adding it incremented by one.
if (coordMap.containsKey(coordsConcat)) {
Integer tempCount = coordMap.get(coordsConcat);
tempCount = tempCount + 1;
coordMap.remove(coordsConcat);
coordMap.put(coordsConcat, tempCount);
} else {
coordMap.put(coordsConcat, 1);
}
}
我遇到的问题是 containsKey 总是返回 false,即使通过测试我输入了两个相同的经度和纬度坐标。
编辑:我尝试addCords(0,0);
了 5 次,但 HashMap 仍然是空的。
编辑 2:双值仅到千位。
测试用例:
GeoState test = new GeoState("MA");
test.addCoords(0,0);
test.addCoords(0,0);
test.addCoords(0,0);
test.addCoords(0,0);
System.out.println(test.getRegionCoords());
这将返回{}
谢谢