覆盖哈希码方法时的HashMap性能 [英] HashMap Performance when overriding hashcode method

查看:123
本文介绍了覆盖哈希码方法时的HashMap性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HashMap 中,如果我将自定义对象作为关键字。

In a HashMap, if I put custom objects as a key.

如果我覆盖
hashCode()方法并实现它将值作为' 1 ';会有任何表现受到打击吗?

What would happen if I override hashCode() method and implement it to pass value as '1'; would there be any performance hit ?

如果我更改 hashCode()方法返回随机值使用 Math.random ) function
会发生什么性能?

if I change hashCode() method to return random value using Math.random() function what would happen to performance ?

推荐答案

添加Math.random()不会影响很多性能命中,但是通过random()函数构造哈希码值是一个坏主意。相反,您可以使用一些良好的散列函数来最大限度地减少碰撞,哪些更快。
为了参考,您可以查看一些链接
http:// www。 partow.net/programming/hashfunctions/

Adding Math.random() doesn't affect much performance hit but it is a bad idea to construct hashcode values through random() function. Instead you can use some of the good hashing function to minimize the collision and which are much faster also. For reference you can check out some of the links http://www.partow.net/programming/hashfunctions/

这篇关于覆盖哈希码方法时的HashMap性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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