Hashtable with int array as key in java

Posted by Niels Hansen on Stack Overflow See other posts from Stack Overflow or by Niels Hansen
Published on 2011-01-08T09:20:56Z Indexed on 2011/01/08 9:54 UTC
Read the original article Hit count: 242

Filed under:
|
|

Hey!

I'm trying to make a hashtable in java where the keys are int[], but it dosen't work. I have made a little test program to show my problem:

public class test{
        public static void main(String[] args){
                int[] test0 = {1,1};
                int[] test1 = {1,1};
                Hashtable<int[], String> ht = new Hashtable<int[], String>();
                String s0 = "foo";

                ht.put(test0, s0);

                System.out.println("the result from ht.get(test1)");
                System.out.println(ht.get(test1));
                System.out.println("the result from ht.get(test0)");
                System.out.println(ht.get(test0));
        }
}

My intention is that both ht.get calles should return the same result, since the two arrays are equal, but they dont. Here is the result from running the code:

the result from ht.get(test1)
null
the result from ht.get(test0)
foo

Am I missing something here or is it just impossible to use int[] as keys in a hastable?

© Stack Overflow or respective owner

Related posts about java

Related posts about hashtable