java8 String 转为List
要将Java 8中的字符串转换为列表,您可以使用Stream API的split()方法。
下面是一个示例代码:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StringToListExample {
public static void main(String[] args) {
String str = "apple,banana,grape,orange";
// 使用逗号分隔字符串,并将结果转换为列表
List<String> list = Arrays.stream(str.split(","))
.collect(Collectors.toList());
System.out.println(list);
}
}
输出结果:
[apple, banana, grape, orange]
在上面的代码中,我们首先使用split(",")将字符串分割为字符串数组,然后使用Arrays.stream()将数组转换为流。最后,我们使用collect(Collectors.toList())将流转换为列表
原文地址: http://www.cveoy.top/t/topic/ilsJ 著作权归作者所有。请勿转载和采集!