Author: alegru

Flux<String> cities = Flux.fromIterable( new ArrayList<>(Arrays.asList(“New York”, “London”, “Paris”, “Toronto”, “Rome”))); cities.subscribe(); class ReactiveJavaTutorial { public static void main(String[] args) { Flux<String> cities = Flux.fromIterable( new ArrayList<>(Arrays.asList(“New York”, “London”, “Paris”, “Toronto”, “Rome”))); cities.log().subscribe(); } } Output: INFO 6832 — [ main] reactor.Flux.Iterable.1 : | onSubscribe([Synchronous Fuseable] FluxIterable.IterableSubscription) INFO 6832 — [ main] reactor.Flux.Iterable.1 : |…

Read More How Mono and Flux Work Internally?

class ReactiveJavaTutorial { public static void main(String[] args) { Mono<String> mono = Mono.just(“data”); Flux<String> fluxFromMono = Flux.from(mono); fluxFromMono.subscribe(System.out::println); } } Output: data class ReactiveJavaTutorial { public static void main(String[] args) { // one value Flux<String> flux1 = Flux.just(“data1”); Mono<String> monoFromFlux1 = flux1.next(); // get data from mono monoFromFlux1.subscribe(data -> System.out.println(“monoFromFlux1 data: ” + data)); //…

Read More Convert Mono to Flux and vice versa

class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { Flux<String> flux = Flux.fromArray(new String[]{“data1”, “data2”, “data3”}); flux.subscribe(System.out::println); } } Output: data1 data2 data3   Another way would be using the Reactive Streams operators like onNext, flatMap, etc. class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { Flux<String> flux = Flux.fromArray(new String[]{“data1”,…

Read More Extract data from Flux

class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { // Create a Flux Flux<String> flux = Flux.just(“data1”, “data2”, “data3”); // Subscribe to a Flux and print the elements flux.subscribe(System.out::println); } } Output: data1 data2 data3   When we call the subscribe(), we are telling the Publisher to start sending data. class ReactiveJavaTutorial {…

Read More Subscribe to Flux in Java Reactor

import reactor.core.publisher.Flux; class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { // create an empty Flux Flux flux1 = Flux.just(); // create a Flux that will hold only one value Flux<String> flux2 = Flux.just(“data”); // create a Flux that will hold multiple values Flux<String> flux3 = Flux.just(“data1”, “data2”, “data3”); } } import reactor.core.publisher.Flux;…

Read More Create a Flux in Java Reactor

import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { String dataFromMono = getMono().block(); System.out.println(“Data from Mono: ” + dataFromMono); } private static Mono getMono() { return Mono.fromSupplier(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return “Hello!”; }); } } Output: Data from Mono: Hello! class ReactiveJavaTutorial { public…

Read More Extract Data from Mono in Java

import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { // create a Mono Mono<String> mono = Mono.just(“Hello”); // subscribe to a Mono mono.subscribe(); } } import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { // create a Mono Mono<String> mono = Mono.just(“Hello”); // subscribe to a Mono mono.subscribe(data -> System.out.println(data)); }…

Read More Subscribe to a Mono in Java Reactor

public static <T> Mono<T> just(T data) import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { Mono<String> mono = Mono.just(“Hello”); } } import org.reactivestreams.Publisher; import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { Publisher<String> mono = Mono.just(“Hello”); } } import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { // will…

Read More Create a Mono in Java Reactor

In the previous post, we covered a String to Date conversion. In this post, you will learn to convert LocalDate and LocalDateTime to String in Java. LocalDate and LocalDateTime are immutable date-time objects that represent a date and a date and time. Both of the examples below make use of java.time package. For more information about Java packages, check out our…

Read More Convert LocalDate and LocalDateTime to String in Java

In this lesson, you will learn to convert String to LocalDate and LocalDateTime objects. LocalDate and LocalDateTime are immutable date-time objects representing a date and a date and time. To convert String to one of the two Date objects, the provided String must represent a valid date or time according to ISO_LOCAL_DATE or ISO_LOCAL_DATE_TIME.  Otherwise, a DateTimeParseException will…

Read More Convert String to Date in Java

We can convert boolean to String in Java using the following methods: String.valueOf() method Boolean.toString() method Convert boolean to String in Java using the String.valueOf() method There are multiple overloaded versions of the valueOf() method from the String class. We will use the one which accepts boolean. Example class Test { public static void main(String[] args)…

Read More Convert boolean to String in Java

To convert String to boolean in Java, you can use one of the following methods: Boolean.parseBoolean() method BooleanUtils.toBoolean() method Boolean.valueOf() method if we need a Boolean object instead of a primive Convert String to boolean in Java using the Boolean.parseBoolean() method With the parseBoolean(String s) method of the Boolean class, if we want to get…

Read More Convert Java String to boolean

We can convert Octal to Decimal in Java in the following ways: Using the parseInt() method Using custom logic Convert Octal to Decimal in Java using the parseInt() method Integer class has a method parseInt(String s, int radix) that parses the String argument as a signed integer in the radix specified by the second argument. Example class Test…

Read More Convert Octal to Decimal in Java