编程语言
首页 > 编程语言> > java8 lanbda表达式对list对象去重,对象属性转成string

java8 lanbda表达式对list对象去重,对象属性转成string

作者:互联网



需求:对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(","));

  

标签:getMobilTel,string,Collectors,list,collect,lanbda,new,ulist,stream
来源: https://www.cnblogs.com/tong2018/p/15767039.html