[Java] How to concatenate strings with commas using StringJoiner in java7 environment

2 minute read

Introduction

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

When using StringBuilder to concatenate strings (when generating csv-like characters, etc.), it is easy to do it with StringJoiner if it is **java8 or above. ↓ This kind of thing.

    for (String hoge: list of something) {
      if (sb.length() != 0) {
        sb.append(",");
      }
      sb.append("String to concatenate");
    }

I think that there are other SIer people who are involved in projects below java7 and have similar problems, and create a self-made class that behaves the same as StringJoiner. Then, it has been released as a library so that it can be used under the java7 environment.

Anything is fine, so I just tried to open the library of my own work to the public.

So I made a StringJoiner for java7

I made a class called StringJoiner and published it as a library. As a matter of fact, it is a wrapper class of StringBuilder. It is a class that has a StringBuilder inside and simply concatenates delimiter, prefix and suffix. Click here for the source https://github.com/lovelyswallow/swallowJava8/blob/master/src/main/java/com/lovelyswallow/likejava8/util/StringJoiner.java

Preparation for using StringJoiner in java7

To access the URL below and download the jar directly, just use gradle to get the jar. Since java.util.StringJoiner cannot be used in java7, If you import com.lovelyswallow.likejava8.util.StringJoiner instead, you can use it the same as the standard StringJoiner in java. https://mvnrepository.com/artifact/io.github.lovelyswallow/swallowJava8

Since 1.0 made a mistake and compiled java12, it does not work under the target java7 environment (laugh) Please use 1.1 or above when using. 1.1 and 1.1.1 have the same contents. 1.1.1 just released an empty version because I wanted to act to increase the minor version.

How to use #StringJoiner

Since the same method as Java standard StringJoiner is prepared, you can use it exactly the same. The only difference from the java standard is the type of constructor. It may not be necessary to explain it further, but 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(",");
    sj1.add("hoge");
    sj1.add("fuga");
    sj1.add("piyo");
    System.out.println(sj1.toString()); // => hoge,fuga,piyo

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

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

Finally

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

  • SIer person
  • Participating in the site of java7
  • Cannot upgrade java version 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 problems.

In the future, in addition to StringJoiner, I would like to create something like java8 for java7 environment. However, it may be subtle if you can’t use a lambda expression where you make something that behaves like Stream. .. ..

Good luck Japan :poop: