java中hashcode方法的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18078779/
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
What is the purpose of hashcode method in java?
提问by MaheshVarma
When we have equals()
, compareTo()
methods why there is a hashcode()
method in Java?
当我们有equals()
, compareTo()
methods 为什么hashcode()
Java 中有方法?
And in case if we use HashTable
we have to override hashcode()
method, Is there any special reason except fast accessing of random keys? If we override the hashcode()
method what would be the probable implementation ?
如果我们使用 HashTable
我们必须重写hashcode()
方法,除了快速访问随机密钥之外还有什么特殊原因吗?如果我们覆盖该hashcode()
方法,可能的实现是什么?
How Java ensures object uniqueness in memory?
Java 如何确保对象在内存中的唯一性?
Hashcodes are typically used to enhance the performance of large collections of data
.
Hashcodes are typically used to enhance the performance of large collections of data
.
In hashing
we calculate hash code
. it's an additional task. When we do additional operation for each object that is added to a collection. How the performance gets improved?
在hashing
我们计算hash code
. 这是一项额外的任务。当我们对添加到集合中的每个对象进行附加操作时。性能如何提升?
采纳答案by Marko Topolnik
You must always override equals
and hashCode
in tandem, to satisfy their interdependent contracts. A class which implements them contradictorily is simply broken and unacceptable under even the minimum software engineering standards.
您必须始终覆盖equals
和hashCode
串联,以满足它们相互依赖的合同。即使在最低的软件工程标准下,一个相互矛盾地实现它们的类也只是被破坏和不可接受的。
As to whyone would ever use the hashtable data structure: because it is the fastest option around for random-access key-value storage.
至于为什么会使用哈希表数据结构:因为它是随机访问键值存储的最快选择。
回答by Sachin Verma
why there is a hashcode() method in Java?
Basically whenever we insert in a unique data structure, the data structure make sure that there is no duplicate object is inserted. How do it do that??
This is done by the contract the objects are implementing, hashcode()
that the unique id as SSN of a person. But if you want to retrieve a particular object then guess what should be called after matching SSN, yes you guessed right it is equals()
.
基本上每当我们插入一个唯一的数据结构时,该数据结构确保没有插入重复的对象。它是怎么做到的??
这是由对象正在实现的合同完成的,hashcode()
即一个人的唯一 ID 作为 SSN。但是如果你想检索一个特定的对象,那么猜猜匹配 SSN 后应该调用什么,是的,你猜对了它是equals()
.
回答by Ajay Bhojak
Check the following Link to understand why hashing is used
检查以下链接以了解使用散列的原因
1: http://java.dzone.com/articles/java-hashingand the following Link also wil help you.
回答by Joni
Using the compareTo
method you establish a "total order"for your objects. A total order is a fairly weak property: it can only tell you if one object is "less than" another, but it gives you no idea of "how far apart" two objects are.
使用该compareTo
方法为对象建立“总顺序”。全序是一个相当弱的属性:它只能告诉您一个对象是否“小于”另一个对象,但它无法让您知道两个对象“相距多远”。
For example, if you have a N objects in a key-value data structure and you want to find the value for a given key. Having only a total order you need at least O(log N) comparisons to find a matching key.
例如,如果您在键值数据结构中有 N 个对象,并且您想查找给定键的值。只有一个总订单,您至少需要 O(log N) 次比较才能找到匹配的键。
A hash code is a stronger property because it can tell you if two objects are somewhat similar or completely different. Thanks to this a hash table can find a value for a key with O(1) operations.
哈希码是一种更强的属性,因为它可以告诉您两个对象是有些相似还是完全不同。由于这个原因,哈希表可以通过 O(1) 操作找到键的值。