1.使用谷歌的Joiner转换
public static <T> String parseListToStr(List<T> list){
String result = Joiner.on(",").join(list);
return result;
}
2.使用lambda表达式遍历集合
public static <T> String parseListToStr2(List<T> list){
StringBuffer sb = new StringBuffer();
list.stream().forEach(str->{
sb.append(str).append(",");
});
sb.deleteCharAt(sb.length()-1);
return sb.toString();
}
3.直接使用stream流实现
public static <T> String parseListToStr3(List<T> list){
String result = list.stream().map(String::valueOf).collect(Collectors.joining(","));
return result;
}
4.使用for循环遍历集合
public static <T> String parseListToStr(List<T> list){
StringBuffer sb = new StringBuffer();
if(listIsNotNull(list)) {
for(int i=0;i<=list.size()-1;i++){
if(i<list.size()-1){
sb.append(list.get(i) + ",");
}else {
sb.append(list.get(i));
}
}
}
return sb.toString();
}