Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Flux.concat and Flux.concatWith

I am new to reactive streams and learning the combine two publishers (Flux to be specific) using the concat/concatWith methods.

Everything which i can do with concat method, the same can be achieved using the concatWith method. Here is the sample cases which i used:

        Mono<String> mono1 = Mono.just(" karan ");
        Mono<String> mono2 = Mono.just(" | verma ");
        Mono<String> mono3 = Mono.just(" | kv ");

        Flux<String> flux1 = Flux.just(" {1} ","{2} ","{3} ","{4} " );
        Flux<String> flux2 = Flux.just(" |A|"," |B| "," |C| ");

        // FLux emits item each 500ms
        Flux<String> intervalFlux1 = Flux.interval(Duration.ofMillis(1000))
                                        .zipWith(flux1, (i, string) -> string);

        // FLux emits item each 700ms       
        Flux<String> intervalFlux2 = Flux
                                .interval(Duration.ofMillis(1000))
                                .zipWith(flux2, (i, string) -> string);



        System.out.println("**************Flux Concat***************");
        Flux.concat(mono1, mono2, mono3).subscribe(System.out::print);
        System.out.println();
        Flux.concat(flux2, flux1).subscribe(System.out::print);
        System.out.println();
        Flux.concat(intervalFlux2, flux1).subscribe(System.out::print);

        Thread.sleep(5000);

        System.out.println();
        Flux.concat(intervalFlux2, intervalFlux1).subscribe(System.out::print);
        Thread.sleep(10000);
        System.out.println("----------------------------------------");


        System.out.println("**************Flux Concat with***************");
        mono1.concatWith(mono2).concatWith(mono3).subscribe(System.out::print);
        System.out.println();
        flux1.concatWith(flux2).subscribe(System.out::print);

        System.out.println();
        intervalFlux1.concatWith(flux2).subscribe(System.out::print);
        Thread.sleep(5000);

        System.out.println();
        intervalFlux1.concatWith(intervalFlux2).subscribe(System.out::print);
        Thread.sleep(10000);
        System.out.println();

        System.out.println("----------------------------------------");

and here is the output for both the cases:

**************Flux Concat***************
 karan  | verma  | kv 
 |A| |B|  |C|  {1} {2} {3} {4} 
 |A| |B|  |C|  {1} {2} {3} {4} 
 |A| |B|  |C|  {1} {2} {3} {4} ----------------------------------------
**************Flux Concat with***************
 karan  | verma  | kv 
 {1} {2} {3} {4}  |A| |B|  |C| 
 {1} {2} {3} {4}  |A| |B|  |C| 
 {1} {2} {3} {4}  |A| |B|  |C| 
----------------------------------------

and the time complexity was also similar for both cases.

What is the difference between the two? Is there any specific conditions, when concat or concatWith should be used?

like image 243
KayV Avatar asked Oct 27 '25 15:10

KayV


1 Answers

They are equivalent

Java requires that all code be part of a class, so you can't just have Flux concat(Flux, Flux) as a free function, which imo would be the least confusing.

Some people prefer "always member functions" others prefer "static functions when taking two (or more) of same class".

A third alternative would be a constructor of the form Flux::Flux(Flux, Flux) (or Flux::Flux(Flux[]))

like image 62
Caleth Avatar answered Oct 29 '25 19:10

Caleth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!