一句将特定字符连接的String字符串转换为List<Integer>类型

        List<Integer> list = Arrays.asList(ids.split("-")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());

ids代表用 '-' 字符连接数字的字符串,格式如下所示:“1-2-9-15”这种

String类的成员方法split(String reg)将字符串根据参数字符串类型进行分割,得到字符串数组,如上述数组即被分为{"1","2","9","15"}

Arrays.asList(对象数组) 【不可是基础类型,需为包装类】将对象数组转换为List<该类型>。

List之stream()方法 将集合转化为流Stream对象,以便对集合进行操作

map(A -> B)是流对象的方法,遍历流中的数据 s 将其从A形态转换为B形态,例如上述代码就是将字符串转化为Integer类型的Stream对象

collect(Collectors.toList()) Stream类对象,将Stream转换为List类型

原文地址:https://www.cnblogs.com/lbrs/p/13129187.html