将list集合转换为string
时间: 2024-02-11 19:01:07 浏览: 85
以下是三种将List集合转换为String的方法:
1. 传统循环并拼接的方式
```java
List<String> list = Arrays.asList("张三", "李四", "王五", "赵六");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
builder.append(list.get(i));
builder.append(",");
}
String substring = builder.substring(0, builder.length() - 1);
System.out.println("转化后的字符串 : " + substring);
```
2. Java8 String.join()
```java
List<String> list = Arrays.asList("张三", "李四", "王五", "赵六");
String str = String.join(",", list);
System.out.println("转化后的字符串 : " + str);
```
3. Java8 Collectors.joining()
```java
List<String> list = Arrays.asList("张三", "李四", "王五", "赵六");
String str = list.stream().collect(Collectors.joining(","));
System.out.println("转化后的字符串 : " + str);
```
阅读全文