java 将list转换成string
时间: 2023-07-14 13:03:49 浏览: 95
可以使用Java中的join()方法将List转化为String。例如:
```
List<String> list = Arrays.asList("apple", "banana", "orange");
String result = String.join(",", list);
System.out.println(result); // "apple,banana,orange"
```
也可以使用StringBuilder类进行拼接,例如:
```
List<String> list = Arrays.asList("apple", "banana", "orange");
StringBuilder sb = new StringBuilder();
for(String s: list) {
sb.append(s);
sb.append(",");
}
String result = sb.toString();
System.out.println(result); // "apple,banana,orange,"
```
或者使用Java 8 的 Stream API
```
List<String> list = Arrays.asList("apple", "banana", "orange");
String result = list.stream().map(Object::toString).collect(Collectors.joining(","));
System.out.println(result); // "apple,banana,orange"
```
可以根据需要来选择使用哪种方法。
阅读全文