集合转换字符串,逗号分割
public static String split(List<String> list) {
StringBuilder str = new StringBuilder();
for (String s : list) {
if (StrUtil.isNotBlank(str)) {
str.append(",");
}
str.append(s);
}
return str.toString();
}
String join = Joiner.on(",").join(list);
过滤null
Joiner.on(",").skipNulls().join(arr);
Joiner.on(",").useForNull("null").join(arr);
Joiner提供了Map的join功能
Map<String, String> params = new HashMap(8);
params.put("appid", "appid");
params.put("secret", "secret");
params.put("js_code", "jsCode");
params.put("grant_type", "authorization_code");
String join1 = Joiner.on("&").withKeyValueSeparator("=").join(params);
System.out.println("join1 = " + join1);
把(数组)和(集合)结果放入到StringBuilder or StringBuffer中
StringBuilder builder = new StringBuilder();
StringBuilder resultBuilder = Joiner.on(",").appendTo(builder, arr);
stream流
String collect = arr.stream().collect(Collectors.joining(","));
String
String collect = String.join(",", arr);
StringJoiner
StringJoiner joiner = new StringJoiner(",");
joiner.add("usr");
joiner.add("local");
joiner.add("bin");
System.out.println("joiner = " + joiner);
[源码分析]StringJoiner的使用以及源码分析