- Learning RxJava
- Thomas Nield
- 300字
- 2021-07-02 22:22:57
distinctUntilChanged()
The distinctUntilChanged() function will ignore duplicate consecutive emissions. It is a helpful way to ignore repetitions until they change. If the same value is being emitted repeatedly, all the duplicates will be ignored until a new value is emitted. Duplicates of the next value will be ignored until it changes again, and so on. Observe the output for the following code to see this behavior in action:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.just(1, 1, 1, 2, 2, 3, 3, 2, 1, 1)
.distinctUntilChanged()
.subscribe(i -> System.out.println("RECEIVED: " + i));
}
}
The output of the preceding code snippet is as follows:
RECEIVED: 1
RECEIVED: 2
RECEIVED: 3
RECEIVED: 2
RECEIVED: 1
We first receive an emission of 1, which is allowed forward. But the next two 1 are ignored because they are consecutive duplicates. When it switches to 2, that initial 2 is emitted, but the following duplicate is ignored. A 3 is emitted and its following duplicate is ignored as well. Finally, we switch back to a 2 that emits and then a 1 whose duplicate is ignored.
Just like distinct(), you can provide an optional argument for a key through a lambda mapping. In the following code snippet, we execute the distinctUntilChanged() operation with strings keyed on their lengths:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.just("Alpha", "Beta", "Zeta", "Eta", "Gamma",
"Delta")
.distinctUntilChanged(String::length)
.subscribe(i -> System.out.println("RECEIVED: " + i));
}
}
The output of the preceding code snippet is as follows:
RECEIVED: Alpha
RECEIVED: Beta
RECEIVED: Eta
RECEIVED: Gamma
Note that Zeta was skipped because it comes right after Beta, which also is four characters. Delta is ignored as well because it follows Gamma, which is five characters as well.
- JBoss Weld CDI for Java Platform
- 流量的秘密:Google Analytics網(wǎng)站分析與優(yōu)化技巧(第2版)
- 軟件項(xiàng)目估算
- Python從小白到大牛
- 用Flutter極速構(gòu)建原生應(yīng)用
- 零基礎(chǔ)入門(mén)學(xué)習(xí)Python(第2版)
- Building Wireless Sensor Networks Using Arduino
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- Building Dynamics CRM 2015 Dashboards with Power BI
- 3ds Max印象 電視欄目包裝動(dòng)畫(huà)與特效制作
- Learning Splunk Web Framework
- 零基礎(chǔ)C語(yǔ)言學(xué)習(xí)筆記
- R語(yǔ)言與網(wǎng)站分析
- Learning QGIS(Second Edition)
- Elasticsearch實(shí)戰(zhàn)(第2版)