Table of Contents
RxJava Operator (2) Transforming Observables
Home Backend Development PHP Tutorial RxJava Operator (2) Transforming Observables_PHP Tutorial

RxJava Operator (2) Transforming Observables_PHP Tutorial

Jul 12, 2016 am 09:03 AM
android

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>
Copy after login
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>
Copy after login

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>
Copy after login
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>
Copy after login

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>
Copy after login

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>
Copy after login
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>
Copy after login
运行后得到结果如下。可以看到,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>
Copy after login
对其进行订阅

<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>
Copy after login
得到结果如下,可以看到,我们输出了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>
Copy after login
分别对其订阅

<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>
Copy after login
运行结果如下,可以看到第一个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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Motorola Razr 50s shows itself as possible new budget foldable in early leak Motorola Razr 50s shows itself as possible new budget foldable in early leak Sep 07, 2024 am 09:35 AM

Motorola has released countless devices this year, although only two of them are foldables. For context, while most of the world has received the pair as the Razr 50 and Razr 50 Ultra, Motorola offers them in North America as the Razr 2024 and Razr 2

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

See all articles