为什么Java中没有Hashable接口 [英] Why there is no Hashable interface in Java

查看:116
本文介绍了为什么Java中没有Hashable接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Object 具有 hashCode 方法,但是,它仅用于<$ c之类的关联容器中$ c> HashSet 或 HashMap 。它为什么这样设计? 具有 hashCode 方法的Hashable 接口看起来更优雅的解决方案。

Object in Java has hashCode method, however, it is being used only in associative containers like HashSet or HashMap. Why was it designed like that? Hashable interface having hashCode method looks as much more elegant solution.

推荐答案

在我看来,主要论点是可以为任何Java对象计算出定义明确的默认 hashCode ,以及同样明确定义的等于。没有充分的理由拒绝所有对象的这个功能,当然有很多理由来保留它。所以这在我的书中是不费脑筋的。

The major argument seems to me that there is a well-defined default hashCode that can be calculated for any Java object, along with an equally well-defined equals. There is simply no good reason to withhold this function from all objects, and of course there are plenty reasons not to withhold it. So it's a no-brainer in my book.

这篇关于为什么Java中没有Hashable接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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