You can use the map
operator to map the values of a stream to different values based on the outcome for each value from the function passed to map
. The outcome stream is a new copy and will not modify the provided stream of values, the result stream will have the same length of the input stream but may be of different types.
The function passed to .map()
, must return a value.
Example:
List<Integer> numbers = Arrays.asList(1, 2, 3);
Observable.from(numbers)
.map(number -> {
return number.toString(); // convert each integer into a string and return it
})
.subscribe(onNext -> {
System.out.println(onNext); // print out the strings
});
This code will print out
"1"
"2"
"3"
In this example the Observable accepted a List<Integer>
the list will be transformed to a List<String>
in the pipeline and the .subscribe
will emit String
's