对象的** hashCode **函数 - jdk如何使用它?

时间:2016-06-11 09:22:33

标签: java hashtable

我知道无论何时覆盖parseResult: function(result, navNumber) { var self = this; var imagesMatter = $(result).find('img'); var imagesLength = imagesMatter.length; // if there is any image in ajax request then fetch imgs using ajax for show progress bar if (imagesLength >= 1) { var i = 0, complete = 0, temp = 1; navigation.progress.fadeIn(50); imagesMatter.each(function() { var $this = $(this); var URL = $this.attr('src'); $.ajax({ url: URL, xhr: function() { var xhr = new window.XMLHttpRequest(); xhr.addEventListener("progress", function(evt) { if (evt.lengthComputable) { var percentComplete = parseFloat(evt.loaded / evt.total).toFixed(1); temp = complete + (percentComplete / imagesLength); console.log(parseInt(temp * 100) + '%'); navigation.progress.html(parseInt(temp * 100) + '%'); } }, false); return xhr; }, complete: function() { complete = temp; i++; if (i == imagesLength) { self.closeLoading(navNumber); } } }); }); } else { return; } }方法,您都应该覆盖equals方法。

但我不确定的是, JDK 如何使用它?

例如hashCode / HashSet是使用哈希表的set / map实现,那么说这个表使用对象的hash_code作为hash_function的键是正确的吗?

2 个答案:

答案 0 :(得分:1)

  

那么说这个表使用对象的hash_code作为hash_function的关键是正确的吗?

几乎。 hashCode()实际上是哈希函数。所以HashMap每当它试图找到密钥或放入密钥时,它都会调用密钥hashCode()方法并使用它(带有一些位掩码)来在哈希表中找到适当的元素。

另请注意,它不是由JVM直接使用,而是由某些类直接使用。

答案 1 :(得分:0)

the documentation

中可以找到答案
  

如果要将多个映射存储在HashMap实例中,则使用足够大的容量创建映射将允许映射更有效地存储,而不是根据需要执行自动重新散列来扩展表。请注意,使用具有相同hashCode()的许多键是减慢任何哈希表性能的可靠方法。为了改善影响,当键是可比较的时,这个类可以使用键之间的比较顺序来帮助打破关系。

是的,HashMap使用hashCode

您还可以看到the source code,因为JDK是开源的。 (您将在JDK安装中的src.jar中找到它。)

相关问题