Map with Double Array get method returning null

i hate php :

"Map-Double[], Double-" is how the map is created - as a hash map. trying to feed in a Double[] of [0.0, 0.0] into the map to get the value, but the value it returns is null.

    Scanner input = new Scanner(new File(fileName));
    while (input.hasNextLine()) {
        String[] listed = input.nextLine().split("\\s+");
        Double[] key = new Double[2];
        Double value = 0.0;
        for(int i = 0; i<3; i++) {
            if(i<2) {
                key[i] = Double.parseDouble(listed[i]);
            } else {
                value = Double.parseDouble(listed[i]);
            }
        }

This is everything important to the code, it just reads from a file and places it inside a HashMap.

k5_ :

Arrays as Double[] don't have a equals/hashcode methods that considers the actual content of the array entries. These methods in array only match if you use the same instance.

So you can't use an array as key in a HashMap, using a List<Double> as key would work but has other problems. I would recomend a dedicated class with a custom equals()/hashcode() implemntation.

Using equls/hashcode with double is also highly problematic.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=409482&siteId=1