0

I'm trying to add up all the unique coordinates from a list of locations into a single HashMap that has the coordinate as the key, and the count as the value. The key is the longitude and latitude concatenated by the '$' symbol.

//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);
        }


    }

The issue that I am having is that containsKey always return false, even though through testing I entered two of the same longitude and latitude coordinates.

EDIT: I tried addCords(0,0); 5 times, and the HashMap is still empty.

EDIT 2: The double values are only to the thousandth's place.

Test Case:

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());

This will return {} Thanks

Colby
  • 313
  • 4
  • 13
  • 1
    Please show us the content of `coordMap` and show us an example call of `addCoords` with the passed arguments. – Tom Oct 18 '15 at 01:54
  • Looks fine. You have to debug and see the values in your map just before you check `containsKey`. – user1803551 Oct 18 '15 at 01:55
  • Probably due to precision issues you don't get the same string key `coordsConcat` twice, to set and retrieve. – isaias-b Oct 18 '15 at 01:55
  • 1
    Btw you don't need to delete a row from the HashMap to do an update, you can simply use "put", and the existing entry with the same key will be updated. If there's no entry with the key you're putting, then it will be inserted. So put basically is "update or insert". – turingcomplete Oct 18 '15 at 01:55
  • 2
    Try `addCoords(0,0)` as a quick test if precision is the problem. But I would suggest to create a value class for your key type consisting of the both fields; `longitude` and `latitude`. – isaias-b Oct 18 '15 at 01:56
  • I tried addCords(0,0) and the issue persists. – Colby Oct 18 '15 at 02:05
  • Checkout this post on double precision: http://stackoverflow.com/questions/6837007/comparing-float-double-values-using-operator; I will also recommend to create a value class proposed by isi. In the value class you can implement your own equals method that deal with double comparison better. – Bon Oct 18 '15 at 02:06
  • Your issue is probably somewhere else; for example, perhaps you're throwing away your object and making a new, empty one without realizing it. – user2357112 Oct 18 '15 at 02:10
  • Works fine for me. You're deleting the existing value if it is already contained – Yassin Hajaj Oct 18 '15 at 02:20
  • Try printing test.coordMap instead – Yassin Hajaj Oct 18 '15 at 02:20
  • In regards to your "EDIT 2", as I said previously, do a step-by-step debug and see what value is being put (if at all) in when you call `put` and compare it just before calling `containsKey`. – user1803551 Oct 18 '15 at 02:21
  • What's `getRegionCoords`? – user2357112 Oct 18 '15 at 03:27

1 Answers1

1

This is most probably a precision issue. You might want to check the values of coordsConcat before you put them into the HashMap, after you put it into the HashMap and before you check for containsKey.

Odds are there is some minor (or major) mismatch between the values which is causing this issue.