简体   繁体   English

java 流:累积收集器

[英]java streams: accumulated collector

Currently, that's my code:目前,这是我的代码:

Iterable<Practitioner> referencedPractitioners = this.practitionerRepository.findAllById(
    Optional.ofNullable(patient.getPractitioners())
        .map(List::stream)
        .orElse(Stream.of())
        .map(Reference::getIdPart)
        .collect(Collectors.toList())
    );

As you can see, I'm using this.practitionerRepository.findAllById(Iterable<String> ids) , in order to get all using a single communication with database.如您所见,我正在使用this.practitionerRepository.findAllById(Iterable<String> ids) ,以便使用与数据库的单一通信来获取所有内容。

I was trying to change it using this:我试图用这个来改变它:

Optional.ofNullable(patient)
    .map(org.hl7.fhir.r4.model.Patient::getPractitioners)
    .map(List::stream)
    .orElse(Stream.of())
    .map(Reference::getIdPart)
    .collect(????????);

How could I use this.practitionerRepository.findAllById(Iterable<String> ids) into a custom collector in collect method?如何在collect方法this.practitionerRepository.findAllById(Iterable<String> ids)用于自定义收集器?

Remember I need to get all entities at once.请记住,我需要一次获取所有实体。 I can't get them one by one.我不能一个一个地得到它们。

You can use Collectors.collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher) specialized collector for that.您可以为此使用Collectors.collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)专用收集器。

  1. Make a list of IDs using the Collector.toList() collector and then使用 Collector.toList() 收集器制作一个 ID 列表,然后
  2. Pass a reference practitionerRepository::findAllById to convert from List<String> to Iterable<Practitioner>传递一个参考practitionerRepository::findAllById以从List<String>转换为Iterable<Practitioner>

Example:例子:

Iterable<Practitioner> referencedPractitioners = Optional.ofNullable(patient)
      .map(Patient::getPractitioners)
      .map(List::stream)
      .orElseGet(Stream::of)
      .map(Reference::getIdPart)
      .collect(Collectors.collectingAndThen(toList(), practitionerRepository::findAllById));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM