0

hashCode() and equals() methods were not overridden in StringBuffer and StringBuilder , I was trying this piece of code below..

    //StringBuffer does not override equals & hashCode
        //StringBuffer s = new StringBuffer("saral");
        //StringBuffer s1 = new StringBuffer("saral");

        StringBuilder s = new StringBuilder("saral");
        StringBuilder s1 = new StringBuilder("saral");          

        //String s = new String("saral");
        //String s1 = new String("saral");

       HashSet set=new HashSet();
        set.add(s);
        set.add(s1);
        set.add(null);
       System.out.println("There are "+set.size()+" elements in the set.");

The resulting out come I ma getting is 3 in both the case when I use StringBuffer or StringBuilder but 2 in case of string , since string has override the hashCode() and equals() method, please advise.

0

4 Answers 4

5

StringBuilder/StringBuffer were created for more efficient string operations, not for store values. So you shouldn't use it as keys or implement your version. Or simply call toString on it.

Sign up to request clarification or add additional context in comments.

1 Comment

There are other uses for StringBuilder where equals would be useful, such as using CharSequence to avoid allocation. It is unfortunate it is not implemented.
1

You cannot have the hashCode, equals or compareTo depend on any thing which might change after the object is added to the collection. e.g.

StringBuilder s1 = new StringBuilder("saral");
StringBuilder s2 = new StringBuilder("saral");          

HashSet set = new HashSet();
set.add(s1);
set.add(s2);
assert set.size() == 2;

s2.append("-two");
assert set.size() == 2;

If you made s1.equals(s2) there would be only one entry and when you changes s2, what would happen? Would you have one or two entries now?

In the case of StringBuilder the contents can changes so its hashCode and equals doesn't depend on this so it only has the default implementation.

Comments

0

StringBuilder and StringBuffer are mutable sequence of characters and String is immutable.

Comments

0

As Paul Vargas says, StringBuffer/StringBuilder are mutable.

This means that two StringBuilders or two StringBuffers cannot truely said to be equal unless they are actually the same instance. Thus equality by identity is the most appropriate semantic.

The fact that StringBuffers and StringBuilders are mutable also means that they are not suitable for use as value-based keys, etcetera. You should create a String first.

If you really, really need a "mutable string" class that has equals-by-value semantics, you could always copy the code of AbstractStringBuilder ... or implement it over from scratch.

7 Comments

Hi Stephen , thanks a lot so you means to say that i should create a custom class that extends AbstractStringBuilder and override hascode() and equals() as the string class have done..!!
Just because an object is mutable does not mean it should ignore implementing equals. AbstractStringBuilder does not expose the char[].
@NateS - I didn't say anyone should IGNORE implementing equals. What I said was that equality by identity is the MOST APPROPRIATE definition of equality for these classes. Please try to understand what I'm saying before downvoting.
@StephenC, no offense, but I think the StringBuilder should have equals/hashCode, that the most appropriate equals semantic depends on the situation and should be decided by the developer. There are certainly cases where StringBuilder equality based on the actual textual content is desired, such as an API designed for CharSequence to avoid allocation (sometimes it does matter, eg Android games must avoid GC). Even if you disagree, java.lang.AbstractStringBuilder is default access, so subclassing is not an option. I admit I stated this poorly in my initial downvote clarification comment.
There is no point in arguing with you about it. 1) I'm just pointing out that it was not done that way by accident or through oversight or carelessness. 2) The reason AbstractStringBuilder's access is package private is because they think it would be problematic for Java to have other code depending on it ... and impeding evolution. There's nothing stopping you from implementing your own string builder from scratch.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.