字符串 "1,2,3,4, ..." 转换成 List<Integer>

List<Integer> categoryids = new ArrayList<>();
String[] idsArr = StringUtils.split(categoryIdsStr, ",");
try {
    categoryids = Arrays.stream(idsArr)
                       .map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList()) // 转换为 List<Integer>
                       .stream().distinct().collect(Collectors.toList()); // 去重操作
} catch (Exception e) {
    throw new DataNonConformityException();
}
posted @ 2022-07-26 17:14  村上春树的叶子  阅读(271)  评论(0编辑  收藏  举报