What is an efficient way to compare StringBuilder objects

As you apparently already know, StringBuilder inherits equals() from java.lang.Object, and as such StringBuilder.equals() returns true only when passed the same object as an argument. It does not compare the contents of two StringBuilders!

If you look at the source, you’ll conclude that the most efficient comparison (that didn’t involve creating any new objects) would be to compare .length() return values, and then if they’re the same, compare the return values of charAt(i) for each character.

Leave a Comment