0
我试图将位置列表中的所有唯一坐标合并到一个HashMap中,该HashMap以坐标为关键字,计数为值。关键是由'$'符号连接的经度和纬度。为什么containsKey方法不能按预期工作?
//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());
这将返回{}
感谢
请向我们展示'coordMap'的内容,并向我们展示一个带有传递参数的'addCoords'示例调用。 – Tom
看起来不错。您必须在检查'containsKey'之前调试并查看地图中的值。 – user1803551
可能由于精度问题,您无法获得相同的字符串键“coordsConcat”两次,以进行设置和检索。 –