在java Hashmap中将自己的类设置为键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3665404/
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
Setting own class as key in java Hashmap
提问by Nikhil Garg
I have a class which I want to set up as keys in HashMap. I already have implemented the compareTo method for that class. But still when I do:
我有一个类,我想将其设置为 HashMap 中的键。我已经为那个类实现了 compareTo 方法。但是当我这样做时:
map.put(new MyKey(dummyArguements) , dummyValue ); System.out.println(map.get( new MyKey(dummyArguements) ) );
I get null. So that means hashmap is not able to identify that the two keys (for get & put call) are same.
我得到空值。所以这意味着 hashmap 无法识别两个键(用于 get 和 put 调用)是相同的。
Could someone help me here please ?
有人可以帮我吗?
采纳答案by Mike Q
You need to implement hashCode()
and equals()
. compareTo()
is additionally required for sorted map/set.
您需要实施hashCode()
和equals()
。 compareTo()
还需要排序的地图/集。
See this questionfor details.
有关详细信息,请参阅此问题。
回答by whaley
1) In general for collections, what you want to override is the equals() method (and also the hashcode() method) for your class. compareTo()/Comparable and Comparator are typically used for sorting and only take the place of using the equals() method for object equivalance in some cases - examples are implementers of SortedSet such as TreeSet.
1) 一般来说,对于集合,您要覆盖的是类的 equals() 方法(以及 hashcode() 方法)。compareTo()/Comparable 和 Comparator 通常用于排序,并且在某些情况下仅代替使用 equals() 方法进行对象等效 - 示例是 SortedSet 的实现者,例如 TreeSet。
2) Please conform to Java naming standards in your code. Your class names should be capitalized... e.g new MyKey(dummyArguments)
. See http://www.oracle.com/technetwork/java/codeconventions-135099.html#367(and http://www.oracle.com/technetwork/java/codeconvtoc-136057.html) for more detail.
2) 请在您的代码中符合 Java 命名标准。你的类名应该大写......例如new MyKey(dummyArguments)
。有关更多详细信息,请参阅http://www.oracle.com/technetwork/java/codeconventions-135099.html#367(和http://www.oracle.com/technetwork/java/codeconvtoc-136057.html)。
回答by fastcodejava
Do you have the hashCode()
defined? compareTo
is needed for sorting.
你有hashCode()
定义吗?compareTo
需要排序。
回答by u290629
HashMap
doesn't check compareTo()
;
HashMap
不检查compareTo()
;
HashMap
checks hashCode()
and equals()
.
HashMap
检查hashCode()
和equals()
。
回答by amorfis
You should implement equals()
and hashCode()
. Your class should also be immutable. If it is mutable, it's hash code can change after adding it to map. Then the map can have problems finding it.
您应该实施equals()
和hashCode()
。你的类也应该是不可变的。如果它是可变的,它的哈希码可以在将其添加到映射后更改。那么地图在找到它时可能会出现问题。
回答by kostja
When using Collections that rely on hashing like Map and Set you have to implement the equals()
and hashCode()
to guarantee correct functionality. If you don't a new myKey will always be different from the key stored in the map because it uses the default implementations of equals()
and hashCode()
.
当使用像 Map 和 Set 这样依赖散列的集合时,您必须实现equals()
和hashCode()
以保证正确的功能。如果你不是一个新的myKey总是会从存储在地图的关键不同,因为它使用的默认实现equals()
和hashCode()
。
回答by vikash bhorale
You should implement equals() and hashCode(). Your class should also be immutable. If it is mutable, it's hash code can change after adding it to map. Then the map can have problems finding it.
您应该实现 equals() 和 hashCode()。你的类也应该是不可变的。如果它是可变的,它的哈希码可以在将其添加到映射后更改。那么地图在找到它时可能会出现问题。
回答by Sander Hautvast
As of java8 you should also implement Comparable (adding compareTo) because if the number of hash clashes exceeds 11, HashMap stores the entries in a binary tree. If you don't, performance suffers
从 java8 开始,您还应该实现 Comparable(添加 compareTo),因为如果哈希冲突的数量超过 11,HashMap 会将条目存储在二叉树中。如果不这样做,性能会受到影响