且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么StringBuffer / StringBuilder不会覆盖equals或hashCode?

更新时间:2023-02-05 21:53:39

因为 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."