Java使用lambda表达式删除集合元素中为空的值并转为String存储

    public String getString(List<Integer> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        list.removeIf(e -> isNotValid(e));
        return list.stream().map(String::valueOf).collect(Collectors.joining(","));
    }

    private static Boolean isNotValid(Integer in) {
        if (in == null) {//为空的为无效数字
            return true;
        }
        return false;
    }
//输入:[1,2,3,null,5,null,7]
//输出:"1,2,3,5,7"

 

posted @ 2021-10-25 09:56  白玉神驹  阅读(929)  评论(0编辑  收藏  举报