问题描述
为什么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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!