list求差集的方法汇总(两个不同元素的List集合)

发布时间 2023-11-28 14:24:58作者: 郭慕荣

一个全部用户集合List< UserInfo > allUser,一个部分用户集合List< UserInfo > commentUser,

根据UserInfo中的UserID求差集,从allUser中得到剩下的一部分用户,通过stream流和lamda表达式实现

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;
}