两种list求差集的方法

发布时间 2023-04-14 09:11:04作者: 叮当猫_johnny

第一种:
两个UserInfo实体List,
一个全部用户集合List< UserInfo > allUser,
一个部分用户集合List< UserInfo > commentUser,
根据UserInfo中的UserID求差集,从allUser中得到剩下的一部分用户,
通过stream流和lamda表达式实现

  1. public List<UserInfo> getNotComment(List<UserInfo> allUser,List<UserInfo> commentUser){
        List<UserInfo> notCommentUser = allUser.stream()
                 .filter(notComment -> !commentUser.stream().map(all -> all.getUserId()).collect(Collectors.toList()).contains(notComment.getUserId()))
                 .collect(Collectors.toList());
        return notCommentUser;
    }

    两个String型List,

  2. public List<String> getNotComment(List<String> allUser,List<String> commentUser){
    	allUser.removeAll(commentUser);
    	return allUser;
    }