list<String>转字符串
时间: 2023-12-23 14:25:57 浏览: 79
list转String
5星 · 资源好评率100%
以下是三种将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);```
阅读全文