Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
formatting
Source Link
StanislavL
  • 57.5k
  • 9
  • 75
  • 100

hash codehashCode() and equals'equals() methods were not overridden in StringBufferStringBuffer and StringBuilderStringBuilder , 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 iI use string bufferStringBuffer or builderStringBuilder but 2 in case of string , since string has override the hashcode()hashCode() and equals()equals() method, please advise..!!

hash code 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 string buffer or builder but 2 in case of string , since string has override the hashcode() and equals() method, please advise..!!

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.

Source Link
dghtr
  • 581
  • 3
  • 6
  • 20

Regarding methods were not overridden in StringBuffer and StringBuilder?

hash code 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 string buffer or builder but 2 in case of string , since string has override the hashcode() and equals() method, please advise..!!