##和hashCode有什么区别? [英] What is the difference between `##` and `hashCode`?

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

问题描述

方法##hashCode有什么区别?

无论我使用哪个类或hashCode重载,它们似乎都输出相同的值. Google也无济于事,因为它找不到符号##.

They seem to be outputting the same values no matter which class or hashCode overloading I use. Google doesn't help, either, as it cannot find symbol ##.

推荐答案

从散列的角度来看,AnyVal的子类"的行为不正确 :

"Subclasses" of AnyVal do not behave properly from a hashing perspective:

scala> 1.0.hashCode
res14: Int = 1072693248

当然,这是对以下人员的呼叫框:

Of course this is boxed to a call to:

scala> new java.lang.Double(1.0).hashCode
res16: Int = 1072693248

我们可能更愿意这样:

scala> new java.lang.Double(1.0).##
res17: Int = 1

scala> 1.0.##
res15: Int = 1

int 1也是double 1的情况下,我们应该期望这一点.当然,在Java中不会出现此问题.没有它,我们会遇到这个问题:

We should expect this given that the int 1 is also the double 1. Of course this issue does not arise in Java. Without it, we'd have this problem:

Set(1.0) contains 1 //compiles but is false

幸运的是:

scala> Set(1.0) contains 1
res21: Boolean = true

这篇关于##和hashCode有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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