一、使用背景? ? ? ? ??
? ? ? ? 在開發(fā)過程,有時(shí)候需要將集合結(jié)果轉(zhuǎn)化成字符串,按照指定的字符分隔進(jìn)行拼接,java stream的收集器提供相應(yīng)的API,操作簡單方便,下面直接上示例代碼;文章來源地址http://www.zghlxwxcb.cn/news/detail-849312.html
二、API操作
List<String> list = Arrays.asList("a","b","c","d","e","f","g");
String delimiter= "#";
String res1 = list.stream().collect(Collectors.joining(delimiter));
System.out.println("res1="+res1);
/// 如果集合本身就是String類的泛型,那么可以直接使用String.join
String res2 = String.join(delimiter, list);
System.out.println("res2="+res2);
文章來源:http://www.zghlxwxcb.cn/news/detail-849312.html
到了這里,關(guān)于Java Stream 拼接字符串的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!