RxJava2 Flowable distinct(过滤操作符)

distinct(过滤操作符)

目录

1 distinct作用

2 distinct接口

3 distinct图解

4 distinct测试用例


 

1 distinct作用

过滤掉Publisher发射的相同的数据,可用于去重操作

 

2 distinct接口

Flowable<T> distinct()

Returns a Flowable that emits all items emitted by the source Publisher that are distinct based on Object.equals(Object) comparison.

返回一个Flowable,它根据Object.equals(Object)比较发出源Publisher发出的所有项目。

<K> Flowable<T> distinct(Function<? super T,K> keySelector)

Returns a Flowable that emits all items emitted by the source Publisher that are distinct according to a key selector function and based on Object.equals(Object) comparison of the objects returned by the key selector function.

返回一个Flowable,它根据键选择器函数并基于对键选择器函数返回的对象的Object.equals(Object)比较,发出源Publisher发出的所有项目。

<K> Flowable<T> distinct(Function<? super T,K> keySelector, Callable<? extends Collection<? super K>> collectionSupplier)

Returns a Flowable that emits all items emitted by the source Publisher that are distinct according to a key selector function and based on Object.equals(Object) comparison of the objects returned by the key selector function.

3 distinct图解

distinct是去重复操作,将发射的数据与之前发射的数据相比,如果相同就丢掉

RxJava2 Flowable distinct(过滤操作符)

 

4 distinct测试用例

    @Test
    public void distinct() {
        System.out.println("######distinct#####");
        Flowable.just("item1", "item2", "item2", "item3").distinct().subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println("s:" + s);
            }
        });
    }


测试输出
######distinct#####
s:item1
s:item2
s:item3