如何通过一个集合过滤另一个集合?

发布于 2025-01-15 00:05:02 字数 562 浏览 1 评论 0原文

有2套,用户有很多字段,但是必须的就是departmentId,他们还给了我一个id数组用于删除,还有这个那个Strig。如何迭代 oldUser 并仅保留departmentId 位于removeUserById 中的那些用户?下面是我写的流,但它不起作用,所有用户都无一例外地进入 abс 集

Set<User> oldEmploee = userRepository.findAllByDepartmentId(departmentId);
Set<String> newEmploee = command.getEmployees();
Set<User> abc = oldEmploee.stream().filter(e -> !newEmploee.contains(e.getDepartmentId())).collect(Collectors.toSet());

abc.forEach(user -> user.setDepartmentId("3a9b4a8a-9e8c-4b72-b3cd-4e72090c259b"));
userRepository.saveAll(abc);

There are 2 sets, the user has a lot of fields, but the necessary one is the departmentId, they also gave me an id array for deletion, and this and that Strig. How can you iterate over oldUser and leave only those users whose departmentId is in removeUserById? Below is the stream that I wrote But it does not work, all users go to the abс set without exceptions

Set<User> oldEmploee = userRepository.findAllByDepartmentId(departmentId);
Set<String> newEmploee = command.getEmployees();
Set<User> abc = oldEmploee.stream().filter(e -> !newEmploee.contains(e.getDepartmentId())).collect(Collectors.toSet());

abc.forEach(user -> user.setDepartmentId("3a9b4a8a-9e8c-4b72-b3cd-4e72090c259b"));
userRepository.saveAll(abc);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2025-01-22 00:05:02
Set<User> abc = oldEmploee.stream().filter(e -> newEmploee.contains(**e.getId()**)).collect(Collectors.toSet());
Set<User> abc = oldEmploee.stream().filter(e -> newEmploee.contains(**e.getId()**)).collect(Collectors.toSet());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文