java8新特性之逗号分隔字符串转List

业务背景:

某个数据库字段,存储的是逗号分隔的id,可能是Integer也可能是Long型的,比如:1,2,3等;需要转换成Long型的List或者Integer型的List,怎么做更简便??

见代码:

//You can use the Lambda functions of Java 8 to achieve this without looping
//来自:http://stackoverflow.com/questions/19946980/convert-string-to-listlong

String ids= "1,2,3,4,5,6";
List<Long> listIds = Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
System.out.println(Arrays.toString(listIds .toArray()));//[1,2,3,3,4,5,6]

    原文作者:leolu007
    原文地址: https://blog.csdn.net/Leolu007/article/details/53218258
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞