我想更改 Stream
中字段的值。我试图在 .map
中更改它,但出现编译错误
Syntax error on token(s), misplaced construct(s)
流:
user.getMenuAlertNotifications()
.parallelStream()
.filter(not -> not.getUser().getId()==userId &&
notificationList.getIds().contains(not.getId()))
.map(not -> not.setRead(Boolean.TRUE) -> not)
.forEach(not -> menuService.save(not));
最佳答案
您不会转换 Stream<MenuAlertNotification>
到 Stream<Boolean>
, 所以不要使用 map
这应该是一个 non-interfering , stateless操作:
.filter(...)
.forEach(not -> {
not.setRead(Boolean.TRUE);
menuService.save(not);
});
旁注,not
表达了一些人可能会感到困惑或奇怪的负面评论(我确实如此)。我会将该 lambda 参数重命名为 notification
,尽管您可以找到更短的选项。
对了,施工not -> not.set Read(Boolean.TRUE) -> not
可能会被转换成一个完全有效的表达式:
.<Consumer<MenuAlertNotification>>map(not -> n -> n.setRead(Boolean.TRUE))
关于java - 更改 Stream 的 map 函数中字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52400149/