java中hashcode方法的目的是什么? [英] What is the purpose of hashcode method in java?

查看:101
本文介绍了java中hashcode方法的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们有 equals() compareTo()方法为什么有 Java中的hashcode()方法?

When we have equals(), compareTo() methods why there is a hashcode() method in Java?

如果我们使用 HashTable ,我们必须覆盖 hashcode()方法,除快速访问随机密钥外,有什么特殊原因吗?如果我们覆盖 hashcode()方法,那么可能的实现是什么?

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 ?

Java如何确保内存中的对象唯一性?

How Java ensures object uniqueness in memory?

Hashcodes通常用于增强大量数据集的性能

哈希中我们计算哈希码。这是一项额外的任务。当我们为添加到集合的每个对象执行其他操作时。性能如何提高?

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?

推荐答案

您必须始终覆盖等于 hashCode 串联,以满足他们相互依赖的合同。即使是最低限度的软件工程标准,反对实现它们的类也是简单的破坏和不可接受的。

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.

关于为什么可以使用哈希表数据结构:因为它是随机访问键值存储的最快选择。

As to why one would ever use the hashtable data structure: because it is the fastest option around for random-access key-value storage.

这篇关于java中hashcode方法的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆