Friday, December 1, 2023

Reactive Programming - Convert List[Mono[T]] to Mono[List[T]]

Hello world,

It's been a while, but I'm finally get back into a new groove with programming. My hope for the new year (2024) is to write more on my lessons transitioning to reactive programming.

First, what is different between Reactive Programming vs Functional Programming?

I think this author's article does a splendid job in explaining the difference.

Also, what is different between Reactive Programming vs Imperative Programming?

This article is decent but I think JavaScript is the true language to consider the need for reactive programming due to the ability to process data without order (non-sequentially).

Okay, for now, let's dive into a challenge I had this morning (and I'll need to back track throughout next year). My issue this morning dealt with my need to:

Convert a List<Mono<T>> to a Mono<List<T>

Because, my response to the client needs to be in a Mono<T>; not List<T>.

Since I couldn't figure this out on my on with Mono.map(), Mono.flatMap() and Flux.fromIterable(), I used ChatGPT. You can see my exact question and ChatGPT's solution at the bottom of my conversation here.


Solution (from ChatGPT)

---

To convert a `List<Mono<T>>` to a `Mono<List<T>>` in a reactive programming context using Project Reactor in Java, you can use the `Flux.concat` operator along with `collectList`. Here's an example:


import reactor.core.publisher.Flux;

import reactor.core.publisher.Mono;


import java.util.List;


public class MonoListConversion {


    public static void main(String[] args) {

        // Your original List<Mono<T>>

        List<Mono<String>> monoList = getOriginalMonoList();


        // Convert List<Mono<T>> into Mono<List<T>>

        Mono<List<String>> resultListMono = Flux.concat(monoList).collectList();


        // Now, you have Mono<List<T>> as the result

        resultListMono.subscribe(System.out::println);

    }


    private static List<Mono<String>> getOriginalMonoList() {

        // Your logic to obtain List<Mono<T>>

        // For demonstration purposes, let's create a simple example

        Mono<String> mono1 = Mono.just("Value 1");

        Mono<String> mono2 = Mono.just("Value 2");

        return List.of(mono1, mono2);

    }

}


In this example, `Flux.concat` is used to concatenate the values emitted by the `Mono` instances into a single `Flux`. Then, `collectList` is used to collect the values emitted by the `Flux` into a `Mono<List>>`.

This approach ensures that the values emitted by the `Mono` instances are processed sequentially, maintaining the order of the original list. If the order is not important, you can also use `Flux.merge` instead of `Flux.concat` for concurrent processing.

---


15 comments:

  1. This was a helpful read. Technopedia Software Pvt Ltd perfectly aligns with the latest trends in software development and digital transformation.
    Our Social Sites -
    divephotoguide
    clickup
    pinterest
    heylink
    beacons
    provenexpert
    kit
    xero
    medium
    blogger

    ReplyDelete
  2. This blog shares valuable information across trusted platforms like Medium, Quora, Archive, and others, helping users discover informative content, travel insights, and useful resources. It connects readers with reliable sources, guides, and experiences to enhance knowledge, plan better journeys, and explore services with confidence and convenience online.

    Quora |

    Archive |

    Issuu |

    Blogspot |

    Scribd |

    Tripadvisor |

    LiveJournal

    ReplyDelete
  3. This blog highlights useful platforms like ProvenExpert, Strikingly, and Product Hunt, helping users explore digital tools, online profiles, and service reviews. It provides helpful insights, trusted resources, and valuable links to improve online presence, discover new opportunities, and connect with reliable platforms for better growth, visibility, and engagement.

    ProvenExpert |

    Strikingly |

    LeetCode |

    Luma |

    Mssg |

    Magic.ly |

    Stocktwits |

    Product Hunt

    ReplyDelete
  4. This blog connects users with popular platforms like Tumblr, Zupyak, and Mastodon, offering valuable resources, creative content, and useful tools. It helps improve online visibility, discover new opportunities, and access trusted websites for sharing ideas, building profiles, and exploring engaging content across different communities and digital networks.

    PasteLink |

    Tumblr |

    Zupyak |

    AIcrowd |

    Facer |

    Inkbunny |

    Checkli |

    Stylevore |

    Mastodon

    ReplyDelete
  5. This blog shares useful content across popular platforms like Quora, Pinterest, Medium, and others, helping users explore ideas, gain knowledge, and improve online presence. It offers valuable resources, creative insights, and trusted links to connect with audiences, share information, and discover engaging content across multiple digital communities.

    Quora |

    Pinterest |

    SlideShare |

    Issuu Publisher |

    Blogspot |

    Scribd |

    Dribbble

    ReplyDelete
  6. This blog connects users with trusted platforms like LiveJournal, Product Hunt, and ProvenExpert, offering useful resources, reviews, and digital tools. It helps improve online visibility, discover new opportunities, and access reliable websites for sharing content, building profiles, and engaging with active communities across various online networks.

    LiveJournal |

    HeyLink |

    Product Hunt |

    ProvenExpert |

    Mastodon

    ReplyDelete
  7. This blog highlights popular platforms like Medium, Quora, WordPress, and Pinterest, offering valuable content, creative ideas, and trusted resources. It helps users improve online presence, explore informative articles, share knowledge, and connect with audiences through reliable platforms for better engagement, visibility, and growth across digital communities.

    Medium |

    Quora |

    WordPress |

    Pinterest |

    SlideShare |

    Gravatar |

    Archive |

    Issuu

    ReplyDelete
  8. This blog highlights popular platforms like Medium, Quora, WordPress, and Pinterest, offering valuable content, creative ideas, and trusted resources. It helps users improve online presence, explore informative articles, share knowledge, and connect with audiences through reliable platforms for better engagement, visibility, and growth across digital communities.

    Medium |

    Quora |

    WordPress |

    Pinterest |

    SlideShare |

    Gravatar |

    Archive |

    Issuu

    ReplyDelete
  9. This blog highlights popular platforms like Medium, Quora, Scribd, Pinterest, and others, offering valuable content, creative ideas, and trusted resources for users. It helps improve online visibility, share knowledge, and explore useful information across multiple channels. Readers can discover engaging content, build strong profiles, and connect with wider audiences through reliable digital platforms for better growth and meaningful interactions.

    Medium |

    Quora |

    Scribd |

    Pinterest |

    SlideShare |

    Blogger |

    Issuu |

    Dribbble |

    Archive

    ReplyDelete
  10. This blog shares useful travel insights, tour packages, and helpful resources for planning trips easily. It connects readers with trusted platforms, guides, and services to explore destinations like Mathura and Vrindavan with comfort, affordability, and better travel experiences.

    Start Page |

    3D Manager |

    Blog Post |

    Project Page |

    Tour Article |

    Travel Blog |

    Community Post |

    Social Profile

    ReplyDelete
  11. This blog shares helpful travel information, booking guides, and useful resources for planning comfortable journeys. It connects users with trusted platforms, services, and tools to explore travel options, improve planning, and enjoy smooth, affordable, and memorable travel experiences with reliable support and detailed insights.

    Business Inquiry |

    Forum Thread |

    Discussion Post |

    Course Page |

    Project Dashboard |

    Job Submission |

    Support Page |

    Feed Post

    ReplyDelete
  12. This blog shares useful information about tour packages, travel planning, and reliable services for comfortable journeys. It helps users discover trusted platforms, helpful resources, and practical guides to plan trips easily, explore destinations like Vrindavan, and enjoy smooth, affordable, and memorable travel experiences with confidence.

    Tour Dashboard |

    Support Page |

    Activity Feed |

    Course Page |

    Tour Notes |

    Digital Product |

    Flipbook |

    Ads Page

    ReplyDelete
  13. This blog shares helpful insights about taxi services, local sightseeing, and travel planning in Vrindavan. It connects users with trusted resources, useful guides, and reliable platforms to ensure comfortable journeys, affordable bookings, and smooth travel experiences while exploring popular destinations with ease and convenience.

    Ads Listing |

    Flipbook |

    Digital Product |

    Taxi Notes |

    Course Page |

    Sightseeing Feed |

    Support Page |

    Service Dashboard

    ReplyDelete