美文网首页
【积累】- String 工具类"1,2,3,4" 转List<

【积累】- String 工具类"1,2,3,4" 转List<

作者: lconcise | 来源:发表于2019-04-20 19:53 被阅读0次

工作经常会遇到“1,2,3,4”转List,List 转 String。

String convert List<Long> 用JAVA8新特性改进后特别简洁,做个记录。

    /**
     * List<Long> convert String.
     *
     * @param longLists List<Long>
     * @return String   example:"1,2,3,4"
     */
    public String LongListToString(List<Long> longLists) {
        return longLists.toString().replaceAll("\\[|\\]", "");
    }

    /**
     * String convert List<Long>.
     *
     * @param str example:"1,2,3,4"
     * @return List<Long>  List<Long>
     */
    public List<Long> StringToLongList(String str) {
        return Arrays.asList(str.split(","))
                .parallelStream()
                .map(a -> Long.parseLong(a.trim()))
                .collect(Collectors.toList());
    }
}

相关文章

网友评论

      本文标题:【积累】- String 工具类"1,2,3,4" 转List<

      本文链接:https://www.haomeiwen.com/subject/hozgwqtx.html