我需要将Pair<Application, FileObject[]>
的流平放到Pair<Application, FileObject>
的流。
目前,我已对此进行了编码:
List<Application> applications = this.applicationDao.findAll();
applications.stream()
.map(app ->
new Pair<Application, FileObject[]>(
app,
this.massiveInterfaceService.getPendingDocuments(app)
)
);
因此,我需要获取Pair<app, FileObject>
的信息流。
this.massiveInterfaceService.getPendingDocuments
是:
public Stream<FileObject> getPendingDocuments(Application app) { /*...*/ }
有什么想法吗?
答案 0 :(得分:3)
假设massiveInterfaceService.getPendingDocuments()
返回FileObject[]
,则可以创建如下方法:
Stream<Pair<Application, FileObject>> flatten(Pair<Application, FileObject[]> pair) {
return Arrays.stream(pair.getRight())
.map(fileObject -> new Pair.of(pair.getLeft(), fileObject));
}
然后在您的信息流中使用它:
Stream<Pair<Application, FileObject>> stream =
applications.stream()
.map(app ->
Pair.of(app, this.massiveInterfaceService.getPendingDocuments(app)))
.flatMap(this::flatten);
另一方面,如果massiveInterfaceService.getPendingDocuments()
返回Stream<FileObject>
Stream<Pair<Application, FileObject>> stream =
applications.stream()
.flatMap(app ->
this.massiveInterfaceService
.getPendingDocuments(app)))
.map(fileObject -> Pair.of(app, fileObject)));
从您的问题尚不清楚哪个是正确的。
答案 1 :(得分:2)
您可以简单地调用flatMap
,从getPendingDocuments
的结果创建流。这是因为getPendingDocuments
已经返回了流。
applications.stream()
.flatMap(app -> this.massiveInterfaceService
.getPendingDocuments(app)
.map(doc -> Pair.of(app, doc));