How to concatenate strings separated by commas using StringJoiner even in java7 environment


I am currently working for a certain SI company, and I am currently participating in a java maintenance project. The version of the system under maintenance and development is old, and unfortunately I am using java7.

When using StringBuilder to concatenate strings (when generating csv-like characters, etc.), I sometimes think that if it is ** java8 or higher, it can be easily done with StringJoiner ... **. ↓ This is what it is.

    for (String hoge :List of something) {
      if (sb.length() != 0) {
      sb.append("The string you want to concatenate");

I think that there are other SIers who are participating in projects under java7 and have similar problems, so I created a self-made class that behaves the same as ** StringJoiner. However, it has been released as a library so that it can be used under the java7 environment </ font> **.

Anything is fine, so I just wanted to do the act of ** opening my own library to the public **.

So I made my own StringJoiner for java7

I created a class named StringJoiner and published it as a library. It's actually a wrapper class for StringBuilder. It is a class that has StringBuilder internally and just concatenates delimiter, prefix, suffix nicely. Click here for the source ↓

Preparing to use StringJoiner with java7

Just access the URL below and download the jar directly, or get the jar with gradle etc. Since java.util.StringJoiner cannot be used in java7, If you import com.lovelyswallow.likejava8.util.StringJoiner instead, you can use it in the same way as java standard StringJoiner.

I made a mistake in 1.0 and compiled it with ** java12 </ font> **, so it doesn't work under the target java7 environment (laughs). Please use 1.1 or above when using. The contents of 1.1 and 1.1.1 are the same. 1.1.1 was just a blank release because I wanted to do the act of ** raising the minor version **.

How to use StringJoiner

Since it provides the same method as the java standard StringJoiner, it can be used in exactly the same way. The only difference from the java standard is the type of constructor. It may not be necessary to explain further, but for the time being, I wrote an example of how to use it.

import com.lovelyswallow.likejava8.util.StringJoiner;

final class Test {
  public static void main(String[] args) {
    StringJoiner sj1 = new StringJoiner(",");
    System.out.println(sj1.toString()); // => hoge,fuga,piyo

    StringJoiner sj2 = new StringJoiner(",", "prefix", "suffix");
    System.out.println(sj2.toString()); // => prefixhoge,fuga,piyosuffix

    //Since it is actually a wrapper class of StringBuilder, you can specify capacity in the constructor.
    StringJoiner sj3 = new StringJoiner(",", 100);
    StringJoiner sj4 = new StringJoiner(",", "prefix", "suffix", 100);


As I said at the beginning, it is a library for the following people, so it is not very useful now.

  • SIer person
  • Participating in the site of java7
  • Java version cannot be upgraded due to project reasons
  • I want to use the API born in java8

I made this library this time, believing that there are other SIers who have the same troubles.

In the future, I would like to create something java8-like class for java7 environment other than StringJoiner. That said, it might be subtle if you can't use a lambda expression when you make something that behaves like a Stream. .. ..

Good luck Japan: poop:

Recommended Posts