Junior — Middle
How can one transform a stream of objects into another data stream in RxJava?
sobes.tech AI
Answer from AI
In RxJava, the map operator is used to transform a stream of objects into another stream. It takes a function that converts each element of the source stream into a new element of a different type.
Example:
Observable<Integer> numbers = Observable.just(1, 2, 3);
Observable<String> strings = numbers.map(number -> "Number: " + number);
strings.subscribe(System.out::println);
The output in the console will be:
Number: 1
Number: 2
Number: 3
Besides map, there are other operators for transforming streams, such as flatMap for working with nested streams.