java java中以int数组为键的哈希表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4633246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hashtable with int array as key in java
提问by Niels Hansen
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:
我试图在 java 中创建一个哈希表,其中键是 int[],但它不起作用。我做了一个小测试程序来显示我的问题:
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:
我的意图是 ht.get 调用应该返回相同的结果,因为两个数组是相等的,但它们不相等。下面是运行代码的结果:
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?
我在这里遗漏了什么,或者是不可能在 hastable 中使用 int[] 作为键吗?
回答by Peter Lawrey
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)
您可以使用 int[] 作为键,但它必须是相同的数组,而不仅仅是具有相同的内容。(这意味着它不会做你想做的事)
Arrays are not equals() or have the same hashCode() based on their content on if they are the same array.
数组不是 equals() 或基于它们的内容具有相同的 hashCode() ,如果它们是相同的数组。
The only way you can do this is to use a List<Integer> as a key or a wrapper for your int[] e.g. TIntArrayList.
您可以这样做的唯一方法是使用 List<Integer> 作为您的 int[] 的键或包装器,例如 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.
顺便说一句:哈希表是一个遗留类恕我直言,除非必须,否则不要使用它。
回答by jon_darkstar
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[])
如果您选择了后者存在的静态方法Arrays
这里所描述的http://download.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#toString(int从Java静态数组类[])
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?)
现在你可以散列它,等效的数组将散列到相同的东西。但是,您将无法从密钥重新创建数组(除非 Javaeval
现在有某种类型?)
.
.
.
.
.
.
.
.
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...
如果有某种静态 toString 可以做到这一点,我深表歉意。PS - Java 是否有 reduce 函数(和 lambdas)、foreach 循环或 eval(如果需要,用于将键重构为数组)?他们会让这个解决方案更好......
回答by u290629
Root cause is array test0and test1have different hashCodes. If 2 keys have different hashcodes, they never can be same.
根本原因是数组test0和test1具有不同的 hashCodes。如果 2 个键具有不同的哈希码,则它们永远不会相同。