Hashcode() and equals() for StringBuilder and StringBuffer

StringBuilder and StringBuffer classes doesnt override equals and hashcode() methods in java.

As String class is immutable in java, mutable StringBuilder and StringBuffer were introduced in java to contruct Strings.

equals() and hashcode() method is mainly overridden to support collections so that if i use a map like hashmap i can retrieve my object that i saved using correct key on basis of hashcode() and equals() method.

A key is preferred to be a mutable one so that anytime i can recover my value for that key, now stringbuilder and stringbuffer being mutable are not preferred as keys and hence there is no point of overriding equals() and hashcode() method for them as they shouldnt be used as a candidate for key in collections.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s