0

Hey i just wanted to say that iam a java newbie. so my problem is that the output is: a - null and i dont know why i did change the HashMap from HashMap<Integer[], Integer> testHashMap = new HashMap<>(); to HashMap<Integer, Integer> testHashMap = new HashMap<>(); and then i worked

        HashMap<Integer[], Integer> testHashMap = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                Integer[] someInteger = {i, j};
                testHashMap.put(someInteger, (i + j * 4));
            }
        }
        Integer[] someOtherInteger = {0,0};
        System.out.println("a - " + testHashMap.get(someOtherInteger));

outPut:"a - null" outPut should be :"a - 0"

im sorry if this is like a dumb question.

4 Answers4

2

Java Arrays do not provide an override for the equals method and will just do an identity comparision.

Therefor

new Integer[]{0,0}.equals(new Integer[]{0,0});

will return false and Integer[] simply isn't a usable class to use as a key for a map, because HashMap uses the equals and hashcode methods internally for the keys.

You should consider creating your own custom class for the key that holds those 2 int values and where you override equals and hashcode.

But you can use List and it should work:

HashMap<List<Integer>, Integer> testHashMap = new HashMap<>();
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        List<Integer> someInteger = Arrays.asList(i, j);
        testHashMap.put(someInteger, (i + j * 4));
    }
}
List<Integer> someOtherInteger = Arrays.asList(0,0);
System.out.println("a - " + testHashMap.get(someOtherInteger));

will give you your desired output.

OH GOD SPIDERS
  • 3,091
  • 2
  • 13
  • 16
1

As @vakio explained, hashCode for arrays does not take into account their contents.

If you really need to use a compound key for a map, replace Integer[] key type array with a list List<Integer>:

Map<List<Integer>, Integer> testHashMap = new HashMap<>();
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        List<Integer> someInteger = Arrays.asList(i, j);
        testHashMap.put(someInteger, (i + j * 4));
    }
}
List<Integer> someOtherInteger = Arrays.asList(0, 0);
System.out.println("a - " + testHashMap.get(someOtherInteger)); // a - 0
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
0

Please read here

He said as follows

No way to do that with arrays, because they don't override equals() and hashCode(). You should define your own class wrapping the array, which would override equals() and hashCode() or use a List<Integer> as key instead.

What I want to note you is same.

Use List<Integer> instead of Integer[].

coding monster
  • 384
  • 4
  • 18
0

In the first case, with the array of Integers, what you essentially store as the key for your map is the object reference of {0,0} or {i,j} in general.

When you’re creating someOtherInteger, Java gives you back another object reference - allocates another memory address. In other words, whenever Java creates a new Object, and array is considered an Object and not a primitive, it allocates a different address for it.

That’s why when calling get giving the second value, holding the someOtherInteger array reference, it returns null. This key, reflecting the unique address of someOtherInteger, does not exist in your map.

panagiotis
  • 1,521
  • 1
  • 9
  • 8