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是去重复操作,将发射的数据与之前发射的数据相比,如果相同就丢掉

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值