1 Stream流
public static String parseListToStr(List list){
String result = list.stream().map(String::valueOf).collect(Collectors.joining(","));
return result;
}文章来源地址https://www.toymoban.com/news/detail-624749.html
2 使用谷歌Joiner方法
import com.google.common.base.Joiner;
public static String parseListToStr1(List list){
String result = Joiner.on(",").join(list);
return result;
}
3 循环插入逗号
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表达式遍历并加入符号
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;文章来源:https://www.toymoban.com/news/detail-624749.html
}
到了这里,关于java中将list用逗号隔开拼成字符串的四种方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!