利用stream将字符串 12,45,89 转成List

/**
 * 将字符串 12,45,89 转成List<Long>
 */
@Test
public void test2() {
    String str = "fghj,48,drftyguhji,,";
    List<Long> ids = Stream
            .of(str.split(","))
            //过滤非数字的字符串
            .filter(s -> Pattern.matches("^[1-9]\\d*|0$", s))\
            //.filter(NumberUtils::isDigits)//import org.apache.commons.lang3.math.NumberUtils;
            .map(Long::valueOf)
            .collect(Collectors.toList());
    System.out.println(ids);
    //[48]
}
posted @ 2022-11-03 10:51  进击的小蔡鸟  阅读(52)  评论(0编辑  收藏  举报