Compare StringBuilder example shows how to compare two StringBuilder objects in Java. Since the StringBuilder is a drop-in replacement for StringBuffer, this applies to compare two StringBuffer objects as well.
How to compare two StringBuilder objects in Java?
The StringBuilder (or StringBuffer) class has not implemented the equals
method, it inherits the version defined in the Object class. The equals
method of the Object class compares the object references, not the content.
So if you want to compare two StringBuilder object contents, comparing them with each other using the equals
method does not work as shown in the below code example.
1 2 3 4 5 6 7 8 |
StringBuilder sbld1 = new StringBuilder("Hello"); StringBuilder sbld2 = new StringBuilder("Hello"); if(sbld1.equals(sbld2)){ System.out.println("StringBuilder objects are equal"); }else{ System.out.println("StringBuilder objects are not equal"); } |
Output
1 |
StringBuilder objects are not equal |
As you can see in the above example, even though the contents of both of the StringBuilder objects are the same, the equals
method returned false. The reason is, they both refer to different objects.
How to compare StringBuilder objects using the content?
We can convert the StringBuilder object to String and then use the equals
method of the String class to compare the content as given below.
1 2 3 4 5 6 7 8 |
StringBuilder sbld1 = new StringBuilder("Hello"); StringBuilder sbld2 = new StringBuilder("Hello"); if(sbld1.toString().equals( sbld2.toString() )){ System.out.println("StringBuilder objects are equal"); }else{ System.out.println("StringBuilder objects are not equal"); } |
Output
1 |
StringBuilder objects are equal |
This approach needs an additional step of converting StringBuilder to String which may not be efficient in terms of performance in some cases. Here is the source code of the toString
method of the StringBuilder class.
1 2 3 4 |
public String toString() { // Create a copy, don't share the array return new String(value, 0, count); } |
As you can see, it creates a new String object using the internal character buffer array and returns it.
What is the preferred way to compare?
I would first check if both StringBuilder object has the same length i.e. the same number of characters. It will be a quick operation and will save us from comparing the objects if the length is not the same.
If they do have the same number of characters, I would compare each character of one StringBuilder object to another StringBuilder object as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
public class StringBuilderCompareExample { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("Hello"); StringBuilder sb2 = new StringBuilder("Hello"); System.out.println( equals(sb1, sb2) ); StringBuilder sb3 = new StringBuilder("Hello"); StringBuilder sb4 = new StringBuilder("Hello world"); System.out.println( equals(sb3, sb4) ); } public static boolean equals(StringBuilder sb1, StringBuilder sb2){ /* * If both refer to the same object, they are equal */ if(sb1 == sb2) return true; /* * If any of them is null, then they are not equal. * We know both are not null at this point because of the previous * condition which checks equality. */ if(sb1 == null || sb2 == null) return false; boolean isEqual = true; /* * If both are not same length, they are not equal */ if(sb1.length() == sb2.length()){ /* * Now, compare the characters one by one, and break the loop * as soon as the characters at given position are different. */ for(int i = 0 ; i < sb1.length(); i++){ if(sb1.charAt(i) != sb2.charAt(i)){ isEqual = false; break; } } }else{ isEqual = false; } return isEqual; } } |
Output
1 2 |
true false |
I did not perform any benchmarking but I think this method should perform better than using the toString
method to compare the StringBuilder or StringBuffer objects, especially in the cases where you want to compare lots of StringBuilder objects having content of different lengths.
This example is a part of the Java StringBuffer tutorial and Java StringBuilder tutorial.
Please let me know your views in the comments section below.