1 使用谷歌的Joiner(代码超级短)
1.1 引入依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.0-jre</version>
</dependency>
1.2 示例代码
import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.List;
public class Convert {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(5);
list.add(4);
list.add(1);
System.out.println(Joiner.on(",").join(list));
}
}
2 使用String.join方法(不用需要CharSequence类型的子类才行,并且需要同类型)
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Convert {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(5);
list.add(4);
list.add(1);
System.out.println(String.join(",", list.stream().map(String::valueOf).collect(Collectors.toList())));
}
}
3 使用collect转换
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Convert {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(5);
list.add(4);
list.add(1);
System.out.println(list.stream().map(String::valueOf).collect(Collectors.joining(",")));
}
}