java将list转化为string
时间: 2023-04-30 15:02:57 浏览: 129
可以使用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"
```
可以根据需要来选择使用哪种方法。
阅读全文