stream操作集合

package test002;

/**
 * @author LSG
 * @date 2022-11-02 23:45
 */
public class User {

    Long stuId;
    String stuName;
    Integer stuAge;

    public Long getStuId() {
        return stuId;
    }

    public void setStuId(Long stuId) {
        this.stuId = stuId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Integer getStuAge() {
        return stuAge;
    }

    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }

    public User() {
    }

    public User(Long stuId, String stuName, Integer stuAge) {
        this.stuId = stuId;
        this.stuName = stuName;
        this.stuAge = stuAge;
    }

    @Override
    public String toString() {
        return "User{" +
                "stuId=" + stuId +
                ", stuName='" + stuName + '\'' +
                ", stuAge=" + stuAge +
                '}';
    }
}
package test002;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 旧集合中没有,新集合中有,需要在旧集合新增
 * 旧集合中有,新集合中没有,需要在旧集合删除
 * 旧集合中有,新集合中有,需要在旧集合更新
 * @author LSG
 * @date 2022-11-02 23:47
 */
public class Test {
    public static void main(String[] args) {
        List<User> newUsers = new ArrayList<>(4);
        User user1 = new User(10L,"aaa",10);
        User user2 = new User(11L,"bbb",10);
        User user3 = new User(12L,"eee",10);
        newUsers.add(user1);
        newUsers.add(user2);
        newUsers.add(user3);
        List<User> oldUsers = new ArrayList<>(4);
        User user4 = new User(13L,"aaa",10);
        User user5 = new User(14L,"eee",10);
        User user6 = new User(14L,"ccc",10);
        oldUsers.add(user4);
        oldUsers.add(user5);
        oldUsers.add(user6);
        List<User> insertMaterials = newUsers.stream()
                .filter(a-> oldUsers.stream()
                        .map(User::getStuName)
                        .noneMatch(stuName-> Objects.equals(a.getStuName(),stuName)))
                .collect(Collectors.toList());
        System.out.println(insertMaterials);

        List<User> updateMaterials = newUsers.stream()
                .filter(a-> oldUsers.stream()
                        .map(User::getStuName)
                        .anyMatch(stuName-> Objects.equals(a.getStuName(),stuName)))
                .collect(Collectors.toList());
        System.out.println(updateMaterials);

        List<User> deleteMaterials = oldUsers.stream()
                .filter(a-> newUsers.stream()
                        .map(User::getStuName)
                        .noneMatch(stuName-> Objects.equals(a.getStuName(),stuName)))
                .collect(Collectors.toList());
        System.out.println(deleteMaterials);
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值