search
HomeBackend DevelopmentPHP TutorialRxJava Operator (2) Transforming Observables_PHP Tutorial

RxJava Operator (2) Transforming Observables

In the previous article, we learned how to create an Observable. Just creating an Observable may not satisfy some complex scenarios, so we are likely to The created Observable needs to be installed with some rules and transformed to emit data. In this article, let’s learn how to convert Observable

1. Buffer
As the name suggests, what the Buffer operator has to do is to cache the data of the specified size, and then cache the cached data. Launched as a set. As shown in the picture below, in the first example picture we specify the buffer size to be 3, and send it out after collecting 3 data. In the second picture we add a skip parameter to specify how many times we need to skip each time a collection is sent. data, how to specify count as 2 and skip as 3 in the figure will emit a set containing two data for every 3 pieces of data. If count==skip, we will find that it is equivalent to the first case. .


Buffer can be cached not only through quantity rules, but also through time and other rules, such as stipulating that the cache is emitted once every 3 seconds, etc. See the code below, we create Create two Observables and use buffer to convert them. The first one is cached by quantity, and the second one is cached by time.

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<List<Integer>> bufferObserver() {<br /></li><li>return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).buffer(2, 3);<br /></li><li>}<br /></li><li><br /></li><li>private Observable<List<Long>> bufferTimeObserver() {<br /></li><li>return Observable.interval(1, TimeUnit.SECONDS).buffer(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread());<br /></li><li>}</li></ol>
Subscribe to it

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("buffer");<br /></li><li>mLButton.setOnClickListener(e -> bufferObserver().subscribe(i -> log("buffer:" + i)));<br /></li><li>mRButton.setText("bufferTime");<br /></li><li>mRButton.setOnClickListener(e -> bufferTimeObserver().subscribe(i -> log("bufferTime:" + i)));</li></ol>

The running results are as follows, you can see that the first Observable will be The first two numbers are emitted every 3 numbers; the second Observable will output 2~4 numbers every 3 seconds.


2. FlatMap
FlatMap is a versatile operator that can transform the data according to the rules you want before emitting it. The principle is to convert this Observable into multiple Observables that use the data emitted by the original Observable as the source data, and then integrate and emit the data emitted by these multiple Observables. It should be noted that the final order may be emitted in a staggered manner. If you have strict requirements on the order, you can use the concatmap operator. FlatMapIterable has the same base as FlatMap, except that the multiple Observables it converts use Iterable as the source data.


Next we use FlatMap and FlatMapIterable to create and transform two Observables respectively.

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<String> flatMapObserver() {<br /></li><li>return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).flatMap(integer -> Observable.just("flat map:" + integer));<br /></li><li>}<br /></li><li><br /></li><li>private Observable<? extends Integer> flatMapIterableObserver() {<br /></li><li>return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9)<br /></li><li>.flatMapIterable(<br /></li><li>integer -> {<br /></li><li>ArrayList<Integer> s = new ArrayList<>();<br /></li><li>for (int i = 0; i < integer; i++) {<br /></li><li>s.add(integer);<br /></li><li>}<br /></li><li>return s;<br /></li><li>}<br /></li><li>);<br /></li><li>}</li></ol>
Subscribe to them separately

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("flatMap");<br /></li><li>mLButton.setOnClickListener(e -> flatMapObserver().subscribe(i -> log(i)));<br /></li><li>mRButton.setText("flatMapIterable");<br /></li><li>mRButton.setOnClickListener(e -> flatMapIterableObserver().subscribe(i -> log("flatMapIterable:" + i)));</li></ol>

The result after running is as follows, the first operation The first character adds a flat map string prefix to the emitted data, and the second character expands the data and outputs n n numbers.



3. GroupBy
The GroupBy operator splits the data emitted by the original Observable into some small Observables according to the key, and then these small Observables emit their respective The data contained is similar to groupBy in SQL.
In use, we need to provide a rule for generating keys. All data with the same key will be included in the same small Observable. In addition, we can also provide a function to convert these data, which is similar to integrating flatMap.

Next, create two Observable objects converted by groupBy. The first one is grouped according to odd and even numbers, and the second grouping adds a string prefix to the number

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("groupBy");<br /></li><li>mLButton.setOnClickListener(e -> groupByObserver().subscribe(new Subscriber<GroupedObservable<Integer, Integer>>() {<br /></li><li>@Override<br /></li><li>public void onCompleted() {<br /></li><li><br /></li><li>}<br /></li><li><br /></li><li>@Override<br /></li><li>public void onError(Throwable e) {<br /></li><li><br /></li><li>}<br /></li><li><br /></li><li>@Override<br /></li><li>public void onNext(GroupedObservable<Integer, Integer> groupedObservable) {<br /></li><li>groupedObservable.count().subscribe(integer -> log("key" + groupedObservable.getKey() + " contains:" + integer + " numbers"));<br /></li><li>}<br /></li><li>}));<br /></li><li>mRButton.setText("groupByKeyValue");<br /></li><li>mRButton.setOnClickListener(e -> groupByKeyValueObserver().subscribe(new Subscriber<GroupedObservable<Integer, String>>() {<br /></li><li>@Override<br /></li><li>public void onCompleted() {<br /></li><li><br /></li><li>}<br /></li><li><br /></li><li>@Override<br /></li><li>public void onError(Throwable e) {<br /></li><li><br /></li><li>}<br /></li><li><br /></li><li>@Override<br /></li><li>public void onNext(GroupedObservable<Integer, String> integerIntegerGroupedObservable) {<br /></li><li>if (integerIntegerGroupedObservable.getKey() == 0) {<br /></li><li>integerIntegerGroupedObservable.subscribe(integer -> log(integer));<br /></li><li>}<br /></li><li>}<br /></li><li>}));<br /></li><li>}</li></ol>

The running results are as follows, we get the desired results.



4. Map, Cast
The function of the Map operator is similar to FlatMap. The difference is that it converts data directly, while FlatMap requires Some intermediate Observables to do this.

Cast forces the data emitted by Observable into another type, which is a specific implementation of Map

Next we create two conversions through map and cast The Observable object

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<Integer> mapObserver() {<br /></li><li>return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).map(integer -> integer * 10);<br /></li><li>}<br /></li><li><br /></li><li>private Observable<Dog> castObserver() {<br /></li><li>return Observable.just(getAnimal())<br /></li><li>.cast(Dog.class);<br /></li><li>}<br /></li><li><br /></li><li>Animal getAnimal() {<br /></li><li>return new Dog();<br /></li><li>}<br /></li><li><br /></li><li>class Animal {<br /></li><li>protected String name = "Animal";<br /></li><li><br /></li><li>Animal() {<br /></li><li>log("create " + name);<br /></li><li>}<br /></li><li><br /></li><li>String getName() {<br /></li><li>return name;<br /></li><li>}<br /></li><li>}<br /></li><li><br /></li><li>class Dog extends Animal {<br /></li><li>Dog() {<br /></li><li>name = getClass().getSimpleName();<br /></li><li>log("create " + name);<br /></li><li>}<br /></li><li><br /></li><li>}</li></ol>
registers it

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("Map");<br /></li><li>mLButton.setOnClickListener(e -> mapObserver().subscribe(i -> log("Map:" + i)));<br /></li><li>mRButton.setText("Cast");<br /></li><li>mRButton.setOnClickListener(e -> castObserver().subscribe(i -> log("Cast:" + i.getName())));</li></ol>
运行后得到结果如下。可以看到,map操作符将数据都乘以10后再发射出来,cast操作符将Animal类型的对象强制转化为Dog类型的对象。另外我们还可以验证一下一个知识点,有继承的情况下创建对象会首先调用父类的构造方法哦。


五、Scan
Scan操作符对一个序列的数据应用一个函数,并将这个函数的结果发射出去作为下个数据应用这个函数时候的第一个参数使用,有点类似于递归操作

下面我们通过一个存放10个2的list创建一个Observable对象并使用scan对其进行转化,转化的函数就是计算的结果乘以下一个数。

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<Integer> scanObserver() {<br /></li><li>return Observable.from(list).scan((x, y) -> x * y).observeOn(AndroidSchedulers.mainThread());<br /></li><li>}</li></ol>
对其进行订阅

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("scan");<br /></li><li>mLButton.setOnClickListener(e -> scanObserver().subscribe(i -> log("scan:" + i)));</li></ol>
得到结果如下,可以看到,我们输出了2的n次方。


六、Window
Window操作符类似于我们前面讲过的buffer,不同之处在于window发射的是一些小的Observable对象,由这些小的Observable对象来发射内部包含的数据。同buffer一样,window不仅可以通过数目来分组还可以通过时间等规则来分组

下面我们创建两个Observable对象分别使用window的数目和时间规则来进行分组。

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<Observable<Integer>> windowCountObserver() {<br /></li><li>return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).window(3);<br /></li><li>}<br /></li><li><br /></li><li>private Observable<Observable<Long>> wondowTimeObserver() {<br /></li><li>return Observable.interval(1000, TimeUnit.MILLISECONDS)<br /></li><li>.window(3000, TimeUnit.MILLISECONDS)<br /></li><li>.observeOn(AndroidSchedulers.mainThread());<br /></li><li>}</li></ol>
分别对其订阅

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("window");<br /></li><li>mLButton.setOnClickListener(e -> windowCountObserver().subscribe(i -> {<br /></li><li>log(i);<br /></li><li>i.subscribe((j -> log("window:" + j)));<br /></li><li>}));<br /></li><li>mRButton.setText("Time");<br /></li><li>mRButton.setOnClickListener(e -> wondowTimeObserver().subscribe(i -> {<br /></li><li>log(i);<br /></li><li>i.observeOn(AndroidSchedulers.mainThread()).subscribe((j -> log("wondowTime:" + j)));<br /></li><li>}));</li></ol>
运行结果如下,可以看到第一个Observable对象没次发射出一个包含3个数据的小Observable,第二个Observable对象每隔3秒钟发射出一个包含2~4个数据的Observable对象


Transforming操作符是Rxjava强大之处的重要体现,要灵活使用Rxjava掌握Transforming操作符是必不可少的。

本文的demo程序见github:https://github.com/Chaoba/RxJavaDemo



www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1077811.htmlTechArticleRxJava操作符(二)Transforming Observables 在上一篇文章中,我们了解了如何创建Observable,仅仅创建一个Observable可能无法满足一些复杂的场景,...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools