需求:对ulist中的mobilTel去重合并成逗号分隔的String,以下三行代码含义
参数类型说明: List<User> ulist,String userinfo
1 因为部分用户的mobileTel为空,则先去除空的数据
2 对非空的list进行去重
3 对特定属性进行逗号分隔
ulist = ulist.stream().filter(c -> c.getMobilTel() != null).collect(Collectors.toList()); ulist = ulist.stream().collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getMobilTel))), ArrayList::new)); userInfo = ulist.stream().map(c -> c.getMobilTel()).collect(Collectors.joining(","));
头两行代码可以合并
ulist = ulist.stream().filter(c -> c.getMobilTel() != null).collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getMobilTel))), ArrayList::new)); userInfo = ulist.stream().map(c -> c.getMobilTel()).collect(Collectors.joining(","));