StringJoiner是java8新增的一个类,主要是帮助我们把一个列表拼接字符串,看下面一个
例子:
class="java">StringJoiner joiner = new StringJoiner(",","(",")");
joiner.add("a").add("b").add("c");
joiner.toString();//输出 "(a,b,c)"
有时候我们需要拼接这样的sql, select * from a where id in (1,2,3,4)
再下面这个例子:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
String commaSeparatedNumbers = numbers.stream()
.map(i -> i.toString())
.collect(Collectors.joining(", "));
System.out.println(commaSeparatedNumbers);//1, 2, 3, 4
java8对String增加了一个join方法:
List<String> list = Arrays.asList("apple","banana","orange") ;
String message = String.join("-", list);
//message 的值是: "apple-banana-orange"
还可以这样写:
String.join("-", "apple","banana","orange")
参考文档:
http://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html