java 流:累积收集器
java streams: accumulated collector
目前,这是我的代码:
Iterable<Practitioner> referencedPractitioners = this.practitionerRepository.findAllById(
Optional.ofNullable(patient.getPractitioners())
.map(List::stream)
.orElse(Stream.of())
.map(Reference::getIdPart)
.collect(Collectors.toList())
);
如您所见,我正在使用 this.practitionerRepository.findAllById(Iterable<String> ids)
,以便使用与数据库的单一通信来获取所有信息。
我试图用这个来改变它:
Optional.ofNullable(patient)
.map(org.hl7.fhir.r4.model.Patient::getPractitioners)
.map(List::stream)
.orElse(Stream.of())
.map(Reference::getIdPart)
.collect(????????);
如何在 collect
方法中将 this.practitionerRepository.findAllById(Iterable<String> ids)
用于自定义收集器?
记住我需要一次获取所有实体。我无法一一得到它们。
您可以为此使用 Collectors.collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
专门的收集器。
- 使用 Collector.toList() 收集器制作 ID 列表,然后
- 传递引用
practitionerRepository::findAllById
以从 List<String>
转换为 Iterable<Practitioner>
示例:
Iterable<Practitioner> referencedPractitioners = Optional.ofNullable(patient)
.map(Patient::getPractitioners)
.map(List::stream)
.orElseGet(Stream::of)
.map(Reference::getIdPart)
.collect(Collectors.collectingAndThen(toList(), practitionerRepository::findAllById));
目前,这是我的代码:
Iterable<Practitioner> referencedPractitioners = this.practitionerRepository.findAllById(
Optional.ofNullable(patient.getPractitioners())
.map(List::stream)
.orElse(Stream.of())
.map(Reference::getIdPart)
.collect(Collectors.toList())
);
如您所见,我正在使用 this.practitionerRepository.findAllById(Iterable<String> ids)
,以便使用与数据库的单一通信来获取所有信息。
我试图用这个来改变它:
Optional.ofNullable(patient)
.map(org.hl7.fhir.r4.model.Patient::getPractitioners)
.map(List::stream)
.orElse(Stream.of())
.map(Reference::getIdPart)
.collect(????????);
如何在 collect
方法中将 this.practitionerRepository.findAllById(Iterable<String> ids)
用于自定义收集器?
记住我需要一次获取所有实体。我无法一一得到它们。
您可以为此使用 Collectors.collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
专门的收集器。
- 使用 Collector.toList() 收集器制作 ID 列表,然后
- 传递引用
practitionerRepository::findAllById
以从List<String>
转换为Iterable<Practitioner>
示例:
Iterable<Practitioner> referencedPractitioners = Optional.ofNullable(patient)
.map(Patient::getPractitioners)
.map(List::stream)
.orElseGet(Stream::of)
.map(Reference::getIdPart)
.collect(Collectors.collectingAndThen(toList(), practitionerRepository::findAllById));