可以使用如下代码实现:

String str = '1,2,3,4,5';
List<Integer> list = Arrays.stream(str.split(","))
        .map(Integer::parseInt)
        .collect(Collectors.toList());

首先将字符串按照逗号分割成字符串数组,然后使用stream流将每个字符串转为Integer类型,并收集成一个List类型的集合。其中,map方法将每个字符串转为Integer类型的方法使用了方法引用的方式,即Integer::parseInt,也可以写成lambda表达式形式:s -> Integer.parseInt(s)。

Java 将字符串用逗号分割并转换为 Integer 列表

原文地址: https://www.cveoy.top/t/topic/ouEs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录