Briefly:
Requirements understanding:
Among the many operators of rxjava, there are data transformation, filtering, etc., but sometimes there are some judging operations? For example, to judge whether all data of an Observable meets a specified condition, to judge whether an Observable will emit a specified value, etc., the answer is yes. Rxjava provides a series of Boolean condition judgment operators.
The Boolean operators in Rxjava mainly include:
- All: judge whether all data items meet a certain condition.
- Contains: determines whether the Observable will emit a specified value.
- IsEmpty: judge whether the original Observable does not transmit any data.
- Sequence equal: determine whether the sequence emitted by two Observables is equal.
1. All
Determine whether all data items meet a certain condition.
Parsing: pass a predicate function to the all operator, which accepts the data emitted by the original Observable and returns a Boolean value according to the calculation. All returns an Observable that only emits a single Boolean value. If the original Observable terminates normally and each data meets the condition, it returns true. If any data of the original Observable does not meet the condition, it returns false.
Example code:
/** * all(Predicate predicate) * Judge whether all data items meet the conditions through the predicate function, and then return a judgment result to the observer */ Observable.just(1, 2, 3, 4, 5) .all(new Predicate<Integer>() { @Override public boolean test(Integer integer) throws Exception { return integer > 5; // Determine whether all data items in the original data item are greater than 5 } }) .subscribe(new SingleObserver<Boolean>() { @Override public void onSubscribe(Disposable d) { System.out.println("--> onSubscribe"); } @Override public void onSuccess(Boolean aBoolean) { System.out.println("--> onSuccess: " + aBoolean); } @Override public void onError(Throwable e) { System.out.println("--> onError: " + e); } });
Output:
--> onSubscribe --> onSuccess: false
Javadoc: all(Predicate predicate)
2. Contains
Determines whether Observable will emit a specified value.
Resolution: pass a specified value to Contains. If the original Observable transmits that value, the Observable it returns will emit true. Otherwise, it will emit false.
Example code:
/** * contains(Object element) * Determine whether the original Observable transmits the specified element data */ Observable.just(1, 2, 3, 4, 5) .contains(5) // Judge whether there is data item 5 in the original data item .subscribe(new SingleObserver<Boolean>() { @Override public void onSubscribe(Disposable d) { System.out.println("--> onSubscribe"); } @Override public void onSuccess(Boolean aBoolean) { System.out.println("--> onSuccess: " + aBoolean); } @Override public void onError(Throwable e) { System.out.println("--> onError: " + e); } });
Output:
--> onSubscribe --> onSuccess: true
Javadoc: contains(Object element)
3. IsEmpty
Determine whether the original Observable does not transmit any data.
Resolution: judge whether the original Observable has sent data items. If the original Observable has sent data, it will send false. Otherwise, it will send true.
Example code:
/** * isEmpty() * Determine whether the original Observable transmits data items. If the original Observable transmits data, it will emit false, otherwise it will emit true. */ Observable.create(new ObservableOnSubscribe<Integer>() { @Override public void subscribe(ObservableEmitter<Integer> emitter) throws Exception { emitter.onComplete(); // No data is transmitted, and the completion notice is directly transmitted } }).isEmpty() .subscribe(new SingleObserver<Boolean>() { @Override public void onSubscribe(Disposable d) { System.out.println("--> onSubscribe"); } @Override public void onSuccess(Boolean aBoolean) { System.out.println("--> onSuccess: " + aBoolean); } @Override public void onError(Throwable e) { System.out.println("--> onError: " + e); } });
Output:
--> onSubscribe --> onSuccess: true
Javadoc: isEmpty()
4. SequenceEqual
Determine whether the sequence emitted by the two Observables is equal.
Resolution: pass two observables to the SequenceEqual operator, which will compare the emitters of two observables. If the two sequences are the same (the same data, the same order, the same termination state), it will emit true, otherwise it will emit false. You can also pass a function to compare two data items or set a cache size to specify the number of items to be prefetched by the first and second source ObservableSource.
Example code:
// Create Observable Observable<Integer> observable1 = Observable.range(1, 10); Observable<Integer> observable2 = Observable.range(1, 10); /** * 1. sequenceEqual(ObservableSource source1, ObservableSource source2) * Compare whether the data items of the two observables are identical (the same data, order, and termination status). If they are the same, send true. Otherwise, send false */ Observable.sequenceEqual(observable1, observable2) .subscribe(new SingleObserver<Boolean>() { @Override public void onSubscribe(Disposable d) { System.out.println("--> onSubscribe(1)"); } @Override public void onSuccess(Boolean aBoolean) { System.out.println("--> onSuccess(1): " + aBoolean); } @Override public void onError(Throwable e) { System.out.println("--> onError(1): " + e); } }); System.out.println("----------------------------------------"); /** * 2. sequenceEqual(ObservableSource source1, ObservableSource source2, BiPredicate isEqual, int bufferSize) * isEqual: // Optional parameter, defining the data item comparison rules of two Observable * bufferSize: // Number of items prefetched from the first and second source ObservableSource * Through the specified comparison function isEqual, compare whether the data items of the two Observable are identical (the same data, order, and termination state), * If it is the same, it will emit true; otherwise, it will emit false. You can also specify a cache size through bufferSize. */ Observable.sequenceEqual(observable1, observable2, new BiPredicate<Integer, Integer>() { @Override public boolean test(Integer t1, Integer t2) throws Exception { System.out.println("--> test(2): t1 = " + t1 + ", t2 = " + t2); return t1 == t2; // Compare whether the data sequence data of two Observable is equal } }, 3).subscribe(new SingleObserver<Boolean>() { @Override public void onSubscribe(Disposable d) { System.out.println("--> onSubscribe(2)"); } @Override public void onSuccess(Boolean aBoolean) { System.out.println("--> onSuccess(2): " + aBoolean); } @Override public void onError(Throwable e) { System.out.println("--> onError(2): " + e); } });
Output:
--> onSubscribe(1) --> onSuccess(1): true ---------------------------------------- --> onSubscribe(2) --> test(2): t1 = 1, t2 = 1 --> test(2): t1 = 2, t2 = 2 --> test(2): t1 = 3, t2 = 3 --> test(2): t1 = 4, t2 = 4 --> test(2): t1 = 5, t2 = 5 --> test(2): t1 = 6, t2 = 6 --> test(2): t1 = 7, t2 = 7 --> test(2): t1 = 8, t2 = 8 --> test(2): t1 = 9, t2 = 9 --> test(2): t1 = 10, t2 = 10 --> onSuccess(2): true
Javadoc: sequenceEqual(ObservableSource source1, ObservableSource source2)
Javadoc: sequenceEqual(ObservableSource source1, ObservableSource source2, BiPredicate isEqual)
Javadoc: sequenceEqual(ObservableSource source1, ObservableSource source2, int bufferSize) Javadoc: sequenceEqual(ObservableSource source1, ObservableSource source2, BiPredicate isEqual, int bufferSize)
Summary
This section mainly introduces that Rxjava boolean (judgment) operators can return boolean type values according to different conditions and make different judgments on Observable.
Tip: Rxjava2 version used above: 2.2.12
Rx introduction and explanation and complete catalog reference: Rxjava2 introduction and detailed examples
Instance code: