Flowable 执行任务和 return 字符串 Rx 列表 Java ReactiveX

Flowable to perform task and return List of String Rx Java ReactiveX

执行任务,最后 return 使用 Flowable rxjva3 的值。我有以下代码

public Maybe<List<String>> uploadObject(Publisher<CompletedFileUpload> images) {
        Storage storage = StorageOptions.getDefaultInstance().getService();
        var returnValue = Flowable.fromPublisher(images)
                .collect((List<String> returnImages, CompletedFileUpload image) -> {
                    BlobId blobId = BlobId.of(googleUploadObjectConfiguration.bucketName(), image.getName());
                    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
                    Blob updatedImage = storage.create(blobInfo, image.getBytes());
                    returnImages.add(updatedImage.getName());
                })
                .flatMapMaybe(returnImages -> Maybe.just(returnImages));
    }

基本上,它迭代并将图像上传到 google 存储。然后 return 媒体 URL 应该 return 到字符串列表。然而尝试了下面的代码,return 类型是 Maybe<U>。执行此操作的正确方法是什么?

更新 1

Flowable.fromPublisher(images).collect(ArrayList::new, (returnImages, image) -> {
            BlobId blobId = BlobId.of(googleUploadObjectConfiguration.bucketName(), image.getName());
            BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
            Blob updatedImage = storage.create(blobInfo, image.getBytes());
            returnImages.add(updatedImage.getName());
            LOG.info(
                    String.format("File %s uploaded to bucket %s as %s", image.getName(),
                            googleUploadObjectConfiguration.bucketName(), image.getName())
            );
        }).flatMapMaybe((returnImages)-> List.of(returnImages));

这也不对,return类型应该是Maybe<List<String>>

根据评论,使用两个参数 collect,然后使用 toMaybe。您可能需要加强集合类型,如下所示:

Flowable.fromPublisher(images)
.<List<String>>collect(ArrayList::new, (returnImages, image) -> {
    BlobId blobId = BlobId.of(googleUploadObjectConfiguration.bucketName(), image.getName());
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
    Blob updatedImage = storage.create(blobInfo, image.getBytes());
    returnImages.add(updatedImage.getName());
    LOG.info(
        String.format("File %s uploaded to bucket %s as %s", image.getName(),
                            googleUploadObjectConfiguration.bucketName(), image.getName())
            );
}).toMaybe();