逗号分割的字符串转换为List集合【lamnbda表达式】
将逗号分割的字符串转换为List数组
例:String s=“1,2,3,4,5,6,7,8,9,0”; 转为list:[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
实现代码:
List<Integer> list = Arrays.asList(s.split(",")).stream().map(x->Integer.parseInt(x)).collect(Collectors.toList());
分布解析:
1.将逗号分割的字符串根据spit分割为字符串数组
String s="1,2,3,4,5,6,7,8,9,0";
String[] split = s.split(",");
2.将字符串数组转为集合
List<String> stringList = Arrays.asList(split);
3.将泛型为String的集合转为Integer集合
List<Integer> list = stringList.stream().map(x->Integer.parseInt(x)).collect(Collectors.toList());
输出结果:
list:[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
-
那么现在我们整合一下就完成将逗号分割的字符串转换为List数组了。
-