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(",");
}
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 **.
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 ↓ https://github.com/lovelyswallow/swallowJava8/blob/master/src/main/java/com/lovelyswallow/likejava8/util/StringJoiner.java
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.
https://mvnrepository.com/artifact/io.github.lovelyswallow/swallowJava8
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 **.
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(",");
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
//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.
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