简体   繁体   中英

Hashtable with int array as key in java

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?

You can use int[] as the key, but it has to be the same array, not just have the same content. (Which means it won't do what you want)

Arrays are not equals() or have the same hashCode() based on their content on if they are the same array.

The only way you can do this is to use a List<Integer> as a key or a wrapper for your int[] eg TIntArrayList.

try the following.

List<Integer> test0 = Arrays.asList(1,1);
List<Integer> test1 = Arrays.asList(1,1);
Map<List<Integer>, String> ht = new HashMap<List<Integer>, String>();

BTW: Hashtable is a legacy class IMHO, don't use it unless you have to.

You can create strings out of the arrays before hashing (unless the length of the array is prohibitively long) in addition to wrapping in a List

Should you choose the latter there is a static method of Arrays described here From java static Arrays class at http://download.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#toString(int[] )

h1.put(Arrays.toString(test1), s0);

Now you can hash this, and equivalent arrays will hash to the same thing. You won't be able to recreate the array from the key, however (unless Java has some kind of eval now?)

.
.
.
.

For curiousities sake, here is me stupidly rolling my own until i found the above method:

public String intArrayToString(int[] x)
{
     String ans = '[';
     for(i = 0; i < size(x); i++)
         ans += '' + i + ',';
     return ans + ']';
}

HashTable<String,String> h1 = new HashTable<String,String> h1;
h1.put(intArrayToString(test1), s0);

If there is some kind of static toString that does this I apologize. PS - does Java have reduce function (and lambdas), foreach loops, or eval (for reconstructing keys into array if need be) yet? They would make this solution nicer...

Root cause is array test0 and test1 have different hashCodes . If 2 keys have different hashcodes, they never can be same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM