为什么StringBuffer / StringBuilder不会覆盖equals或hashCode? [英] Why does StringBuffer/StringBuilder not override equals or hashCode?

查看:232
本文介绍了为什么StringBuffer / StringBuilder不会覆盖equals或hashCode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么StringBuffer / StringBuilder不会覆盖来自对象的 equals() hashcode()方法?

Why does StringBuffer/StringBuilder does not override the equals(), hashcode() methods from object?

请建议我清楚的图片,以帮助理解问题...

Please suggest me clear picture that helps the understand the problem...

推荐答案

因为 StringBuffer 是可变的,其主要用途是构建字符串。如果你想比较内容,请调用 StringBuffer#toString()并比较返回的值。

Because StringBuffer is mutable, and its primary use is for constructing strings. If you want to compare content, call StringBuffer#toString() and compare the returned value.

它不是通常用于覆盖 hashCode()的可变对象,因为修改了这个用作 HashMap中的键的对象可能导致存储的值丢失。

It is not generally useful to override hashCode() for mutable objects, since modifying such an object that is used as a key in a HashMap could cause the stored value to be "lost."

这篇关于为什么StringBuffer / StringBuilder不会覆盖equals或hashCode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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