Java 8 StringJoiner – Old Wine With a New Bottle

SmartTechie_StringJoiner

Ultimately Java 8 is shipped with StringJoiner class under java.util package. I don’t think it is very different implementation to join the strings compared to our old school approach by using StringBuffer/StringBuilder. We will see the usage of the StringJoiner and the internal implementation of it.

For example, I have two strings as “Smart” and “Techie” and I want to join those Strings as [Smart,Techie]. In this case, I have prefix as “[” , suffix as “]” and the delimiter as “,”. StringJoiner has two constructors as given below.


StringJoiner(CharSequence delimiter)


StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

We want to have prefix and suffix, hence we will use the second constructor for our example.

StringJoiner sjr = new StringJoiner(",", "[", "]");
sjr.add("Smart").add("Techie");
System.out.println("The final Joined string is " + sjr);

If we don’t want to have the prefix and suffix, then

StringJoiner sjr1 = new StringJoiner(",");
sjr1.add("Smart").add("Techie");
System.out.println("The final Joined string is " + sjr1);

Now, we will see the implementation of add and toString() method.

public StringJoiner add(CharSequence newElement) {
   prepareBuilder().append(newElement);
   return this;
}

The prepareBuilder() implementation is given below.

private StringBuilder prepareBuilder() {
    if (value != null) {
       value.append(delimiter);
    } else {
       value = new StringBuilder().append(prefix);
    }
    return value;
  }

From the above implementations it is evident that the StringJoiner follows old school approach.

The toString() implementation is given below.

public String toString() {
    if (value == null) {
      return emptyValue;
    } else {
    if (suffix.equals("")) {
      return value.toString();
    } else {
    int initialLength = value.length();
    String result = value.append(suffix).toString();
    // reset value to pre-append initialLength
    value.setLength(initialLength);
    return result;
   }
  }
}

Happy Learning!!!

Advertisements

I am Siva Prasad Rao Janapati. Working as a software developer. Has hands on experience on ATG Commerce(DAS/DPS/DCS), Mozu commerce, Broadleaf Commerce, Java, JEE, Spring, Play, JPA, Hibernate, Velocity, JMS, Jboss, Weblogic,Tomcat, Jetty, Apache, Apache Solr, Spring Batch, JQuery, NodeJS, SOAP, REST, MySQL, Oracle, Mongo DB, Memcached, HazelCast, Git, SVN, CVS, Ant, Maven, Gradle, Amazon Web services, Rackspace, Quartz, JMeter, Junit, Open NLP, Facebook Graph,Twitter4J, YouTube Gdata, Bazzarvoice,Yotpo, 4-Tell, Alatest, Shopzilla, Linkshare. I have hands on experience on open sources and commercial technologies.

Tagged with: ,
Posted in Java
2 comments on “Java 8 StringJoiner – Old Wine With a New Bottle
  1. Alok Chaudhary says:

    Why StringJoiner written in this way?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

DZone

DZone MVB

Java Code Geeks
Java Code Geeks
%d bloggers like this: