1.直接复制以下代码运行查看运行结果
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
public class Test {
public static void main(String[] args) throws Exception {
List<String> strs = new ArrayList<>();
strs.add("abc");
strs.add("def");
strs.add("ghi");
strs.add("jkl");
strs.add("mno");
strs.add("pqr");
String t = "#";//拼接操作符号
String join1 = String.join(t, strs);
System.out.println("join1 = " + join1);
StringJoiner sj = new StringJoiner(t);
strs.forEach(sj::add);
System.out.println("sj = " + sj);
StringBuilder builder = new StringBuilder(t);
strs.forEach(builder::append);
System.out.println("builder = " + builder);
StringBuffer buffer = new StringBuffer(t);
strs.forEach(buffer::append);
System.out.println("buffer = " + buffer);
}
}
四种方法的运行结果都一样,不过推荐使用StringJoiner