英文:
Is there a way to use the containsKey() map function on an Object key?
问题
我有一个包含整数数组作为键和整数作为值的哈希映射。当我向哈希映射中添加条目时,我创建整数数组变量,然后在hashmap.put()
调用中使用变量名。问题是,稍后我需要搜索哈希映射的键,以查看给定的整数数组键是否存在,但hashmap.containsKey()
调用总是返回false,因为哈希映射包含对整数数组的引用,而不是实际的明确整数数组。
是否有一种简单的方法可以使用给定的整数数组搜索哈希映射?
以下是代码片段:
public static void main(String[] args) {
Map<int[], Integer> map = new HashMap<int[], Integer>();
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};
map.put(arr1, 1);
map.put(arr2, 2);
int[] arr3 = {1, 2, 3, 4};
System.out.println(map.containsKey(arr3));
}
请注意,由于Java中的数组是对象,而哈希映射使用对象的引用作为键,因此map.containsKey(arr3)
返回false,因为它不会比较数组的内容。如果您想要使用整数数组作为键来搜索哈希映射,您需要实现自己的逻辑来比较整数数组的内容。
英文:
I have a hashmap with entries containing an integer array as a key and an integer as a value. When I add entires to the hashmap I create integer array variables and then use the variable name in the hashmap.put() call. The problem is that I later need to search the hashmap keys to see if a given integer array key exists, but the hashmap.containsKey() call always returns false because the hashmap contains a reference to the integer array and not an actual explicit integer array.
Is there an easy way I can search the hashmap using a given integer array?
Here is the code fragment:
public static void main (String[] args) {
Map<int[], Integer> map = new HashMap<int[], Integer>();
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};
map.put(arr1, 1);
map.put(arr2, 2);
int[] arr3 = {1, 2, 3, 4};
System.out.println(map.containsKey(arr3));
}
答案1
得分: 3
数组不适合作为HashMap
的键,因为数组不会覆盖默认的equals
和hashCode
实现。因此,即使两个不同的数组对象(比如你的arr1
和arr3
)包含完全相同顺序的相同元素,它们也不相等。
你可以使用List<Integer>
作为键,而不是int[]
。那样会起作用。
英文:
An array is not suitable as a key of a HashMap
, since arrays don't override the default implementation of equals
and hashCode
. Therefore, two different array objects (such as your arr1
and arr3
) are not equal even if they contain the exact same elements and in the same order.
You can use a List<Integer>
as key instead of int[]
. That will work.
答案2
得分: 0
首先,您不能在泛型中使用原始值。您的代码将无法编译。
其次,您永远不应该使用可变值作为哈希表键。
形式上,您可以使用 List<Integer>
代替 int[]
,但这将在将来导致难以调试的错误。当您在哈希表中访问或按键搜索时,会为您的值计算一个哈希码,并在此哈希表已知的列表中进行常量时间的搜索。如果您的代码更改了使用键定义的对象,那么您将不再在哈希表中找到该键值对。
尽管这将解决问题,但我不建议您将 List<Integer>
用作哈希表键。
英文:
<strike>First, you cannot use primitive values in generics. Your code will not compile.</strike>
Second, you should never use mutable values as hash table keys.
Formally, you can use List<Integer>
instead of int[]
, but this will lead to hard-to-debug bugs in the future. When you access or search by key in a hash table, a hash code is calculated for your value and searched for a constant time in the list of known for this hash table. If your code changes the object that you defined with the key, then you will no longer find that key-value pair in the hash table.
I do not recommend that you use List<Integer>
as the hash table key, although this will solve the problem described in the question.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论