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

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

问题描述

##hashCode 方法有什么区别?

What is the difference between methods ## and 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

当然这是装箱到调用:

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天全站免登陆