Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In Java, the StringBuilder class allows us to create a mutable object holding a sequence of characters. This makes it easy to update the object without building it from scratch each time, as would be the case with the standard String class. In this tutorial, we’ll look at how to compare two StringBuilder objects both before and after the release of Java 11.

2. Before Java 11

Before Java 11, there was no built-in comparison method for the StringBuilder. Therefore, we’ll need to write our own. The first obvious check to do is if the two objects being compared are the same length. If not, we can immediately say they don’t match.

Once we know they are the same length, we can use the fact that StringBuilder implements CharSequence to access the charAt() method and compare each character in turn. Let’s combine those two steps into a single method we can use:

boolean compare(StringBuilder one, StringBuilder two) {
    if (one.length() != two.length()){
        return false;
    }
    for (int i = 0; i < one.length(); i++) {
        if (one.charAt(i) != two.charAt(i)) {
            return false;
        }
    }
    return true;
}

We can now use that in a few tests. Let’s see if it confirms two StringBuilders are the same:

@Test
void whenUsingJavaEight_givenTwoIdenticalStringBuilders_thenCorrectlyMatch() {
    StringBuilder one = new StringBuilder("Hello");
    StringBuilder two = new StringBuilder("Hello");
    boolean result = StringBuilderCompare.compare(one, two);
    assertEquals(true, result);
}

To complete the checks, let’s now see that it can detect two which aren’t the same:

@Test
public void whenUsingJavaEight_givenTwoDifferentStringBuilders_thenCorrectlyIdentifyDifference() {
    StringBuilder one = new StringBuilder("Hello");
    StringBuilder two = new StringBuilder("World");
    boolean result = StringBuilderCompare.compare(one, two);
    assertEquals(false, result);
}

The examples in the second test are the same length, so we will be using the charAt() comparisons.

3. After Java 11

With the release of Java 11 StringBuilder now implements Comparable. This gives us access to the compareTo() method which returns an int. The return value will be zero if there’s a match and not zero in the event of the StringBuilders being different. Let’s see that in action with two StringBuilders with the same value:

@Test
void whenUsingJavaEleven_givenTwoIdenticalStringBuilders_thenCorrectlyMatch(){
    StringBuilder one = new StringBuilder("Hello");
    StringBuilder two = new StringBuilder("Hello");
    assertEquals(0, one.compareTo(two));
}

Following that let’s try it with two different values:

@Test
void whenUsingJavaEleven_givenTwoDifferentStringBuilders_thenCorrectlyIdentifyDifference(){
    StringBuilder one = new StringBuilder("Hello");
    StringBuilder two = new StringBuilder("World");
    assertNotSame(0, one.compareTo(two));
}

This is now much easier than before Java 11. The returned integer tells us if the two under comparison are lexicographically different. We’ll be able to tell if the difference was positive or negative as well which may be useful in some applications.

4. Conclusion

In this article, we’ve looked at two ways of comparing StringBuilder objects. We’ve seen that if we are working with a version of Java older than 11, we’ll need to write our own comparison method. However, with Java 11 onwards we are provided with the convenient compareTo() method, which achieves the same result.

As always, the full code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.