【发布时间】:2019-07-20 07:01:05
【问题描述】:
我有一个List<Release>,每个Release 都包含List<Attachment>
我想从每个List<Attachment> 中删除所有附件,除了X 和Y 类型。
我想在 Java 8 中实现这一点。
我尝试了以下代码。但它不起作用。
releases = releases.stream()
.filter(release -> release.getAttachments().stream()
.anyMatch(att -> AttachmentType.X_TYPE.equals(att.getAttachmentType())
|| AttachmentType.Y_TYPE.equals(att.getAttachmentType())))
.collect(Collectors.toList());
【问题讨论】:
-
将
removeIf与倒置条件一起使用。 -
请注意@michalk 回答下的cmets 中指出的
removeIf