java中將list用逗號(hào)隔開拼成字符串的4種方法例子
1 Stream流
public static String parseListToStr(List list){
String result = list.stream().map(String::valueOf).collect(Collectors.joining(","));
return result;
}2 使用谷歌Joiner方法
import com.google.common.base.Joiner;
public static String parseListToStr1(List list){
String result = Joiner.on(",").join(list);
return result;
}3 循環(huán)插入逗號(hào)
public static String parseListToStr2(List list) {
StringBuffer sb=new StringBuffer();
if( listIsNotFull ( list ) ){
for(int i = 0; i < = list.size()-1;i++){
sb.append(list.get(i)+",");
}else{
sb.append(list.get(i));
}
}
return sb.toString();
}4 lambda表達(dá)式遍歷并加入符號(hào)
public static String parseListToStr3(List list){
StringBuffer sb = new StringBuffer();
list.stream().forEach(str - > { sb .append(str). append(",");});
sb.deleteCharAt(sb.length()-1);
return sb.toString();
}5 org.apache.commons.lang.StringUtils lang包下面
public static String parseListToStr3(List list){
String str= StringUtils.join(list,",");
return str;
}拓展: 逗號(hào)分隔的String字符串轉(zhuǎn)成List
public static void main(String args[]) {
String arrayStr="1,2,5,4,1";
//方式1
List<Integer> integerList = Arrays.stream(arrayStr.split(",")).map(s -> Integer.valueOf(s.trim())).collect(Collectors.toList());
//方式2(判空處理,推薦)
List<Integer> integerList1 = StringUtils.isNotBlank(arrayStr)? Stream.of(arrayStr.split(",")).map(Integer::valueOf).collect(Collectors.toList()): Lists.newArrayList();
//方式3
// List<Integer> integerList2 = Arrays.asList((Integer[]) ConvertUtils.convert("1,2".split(","), Integer.class));
System.out.println(integerList1);
System.out.println(integerList);
}總結(jié)
到此這篇關(guān)于java中將list用逗號(hào)隔開拼成字符串的4種方法的文章就介紹到這了,更多相關(guān)java將list用逗號(hào)隔開拼字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus TypeHander不生效的問(wèn)題解決
MyBatis-Plus中使用lambdaUpdate更新JSON字段時(shí),TypeHandler未生效導(dǎo)致數(shù)據(jù)截?cái)噱e(cuò)誤,需手動(dòng)轉(zhuǎn)換或顯式指定typeHandler解決,下面就來(lái)介紹一下該問(wèn)題的解決2025-08-08
java反射機(jī)制給實(shí)體類相同字段自動(dòng)賦值實(shí)例
這篇文章主要介紹了java反射機(jī)制給實(shí)體類相同字段自動(dòng)賦值實(shí)例,具有2020-08-08
Spring Boot Admin監(jiān)控服務(wù)如何使用
這篇文章主要介紹了Spring Boot Admin監(jiān)控服務(wù)如何使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Mybatis返回類型為Map時(shí)遇到的類型轉(zhuǎn)化的異常問(wèn)題
這篇文章主要介紹了Mybatis返回類型為Map時(shí)遇到的類型轉(zhuǎn)化的異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
java開發(fā)ServiceLoader實(shí)現(xiàn)機(jī)制及SPI應(yīng)用
這篇文章主要為大家介紹了java開發(fā)ServiceLoader實(shí)現(xiàn)機(jī)制及SPI應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Xml中使用foreach遍歷對(duì)象實(shí)現(xiàn)代碼
這篇文章主要介紹了Xml中使用foreach遍歷對(duì)象實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
解決沒(méi)有@RunWith 和 @SpringBootTest注解或失效問(wèn)題
這篇文章主要介紹了解決沒(méi)有@RunWith 和 @SpringBootTest注解或失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
解決springmvc+mybatis+mysql中文亂碼問(wèn)題
這篇文章主要介紹了解決java中springmvc+mybatis+mysql中文亂碼問(wèn)題的相關(guān)資料,需要的朋友可以參考下2015-09-09

