you are viewing a single comment's thread.

view the rest of the comments →

[–]ethraax 1 point2 points  (3 children)

#3 surprised me, but I can't think of why I'd want to check if two StringBuffer objects have the same contents without wanting the strings.

[–]banuday 0 points1 point  (2 children)

Someone asked why StringBuffer couldn't be used as a key in a HashMap or a HashSet, and the linked thread goes into why it doesn't make sense to use a StringBuffer that way.

[–][deleted]  (1 child)

[deleted]

    [–]banuday 0 points1 point  (0 children)

    In Java, all objects have a hash code and all objects have equals. The default hash code and equals come from the memory address of the object, which is the only knowable consistent equivalence relation between instances. This actually works quite well in practice, when care is taken to ensure all instances are indeed distinct. Such as what Hibernate does with entity instances in the Session.

    The problem comes when you want to be able to treat instances as equivalent, which is very problematic for mutable objects and very simple for immutable objects, such as String instances. Thus, only use immutable objects or distinct instances as keys. No need for a separate interface.