N.T*_*son 2 java audit java-8 java-stream
总之,以下代码意味着筛选第二个参数中的实体的第一个参数.如果在第二个参数中指定了"更改",则应该过滤较窄的结果.
当我运行它时,我得到一个错误'IllegalStateException:stream已被操作或关闭.
有没有办法可以重用相同的流?
我见过人们实现像Supplier这样的东西,但我认为这不适合这种情况.或者,我只是不熟悉它以了解我如何使用供应商.
/**
* Filters through DocumentAuditEntityListing to find existence of the entities
* ActionEnum, ActionContextEnum, LevelEnum, & StatusEnum.
*
* @param audits A list of audits to search
* @param toFind The audit entities to find
* @return If entities found, return DocumentAudit, else null
*/
public DocumentAudit verifyAudit(DocumentAuditEntityListing audits, DocumentAudit toFind) {
//Filter for proper entities
Stream stream = audits.getEntities().stream().filter(doc -> (
doc.getAction().equals(toFind.getAction())) &&
doc.getActionContext().equals(toFind.getActionContext()) &&
doc.getLevel().equals(toFind.getLevel()) &&
doc.getStatus().equals(toFind.getStatus()));
//If changes were specified, filter further.
if (toFind.getChanges() != null){
stream.filter(change -> (toFind.getChanges().contains(change)));
}
return (DocumentAudit) stream.findFirst().orElse(null);
}
Run Code Online (Sandbox Code Playgroud)
您需要使用分配结果流stream = stream.filter(change -> ...)
.
也使流类型安全(即Stream<DocumentAudit>
).自Java 5以来,泛型一直存在,所以你没有借口使用原始类型.
Stream<DocumentAudio> stream = audits.getEntities().stream()
.filter(doc ->
doc.getAction().equals(toFind.getAction())) &&
doc.getActionContext().equals(toFind.getActionContext()) &&
doc.getLevel().equals(toFind.getLevel()) &&
doc.getStatus().equals(toFind.getStatus());
if (toFind.getChanges() != null){
stream = stream.filter(change -> toFind.getChanges().contains(change));
}
return stream.findFirst().orElse(null);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
78 次 |
最近记录: |