RxJava2 Flowable first 、last (过滤操作符)

本文详细介绍了RxJava2中Flowable的first和last两个过滤操作符。first用于返回源Publisher发射的第一个项目,若为空则抛出异常或指定默认值。last则返回源Publisher发射的最后一个项目,同样处理空值情况。文章包含各操作符的使用场景、接口说明、图解说明及测试用例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

过滤操作符first和last

目录

first

1) first作用和使用场景

2) first接口

3) first图解说明

4) first测试用例

last

1) last作用和使用场景

2) last接口

3) last图解和说明

4) last测试用例


first

1) first作用和使用场景

作用:返回一个新的Single或者Maybe,在源Publisher发射内容不为空的情况下将源Publisher发射的第一个项目作为发射项,如果为空则发射指定的默认项或者NoSuchElementException信号,对于Maybe则直接完成。

使用场景:如果您只对Publisher发出的第一个项目或满足某些条件的第一个项目感兴趣,则可以使用first操作符过滤Publisher。

2) first接口

Single<T>first(T defaultItem)

Returns a Single that emits only the very first item emitted by this Flowable, or a default item if this Flowable completes without emitting anything.

返回一个Single,它仅发出此Flowable发出的第一个项目,如果此Flowable完成而没有发出任何内容,则返回默认项目。

Maybe<T>firstElement()

Returns a Maybe that emits only the very first item emitted by this Flowable or completes if this Flowable is empty.

返回一个Maybe,它只发出此Flowable发出的第一个项,或者如果此Flowable为空则完成。

Single<T>firstOrError()

Returns a Single that emits only the very first item emitted by this Flowable or signals a NoSuchElementException if this Flowable is empty.

返回一个Single,它仅发出此Flowable发出的第一个项,或者如果此Flowable为空则发出NoSuchElementException信号。

 

3) first图解说明

仅仅只是将源Publisher的第一个发射项作为新发射源的唯一发值

 

4) first测试用例

测试代码
@Test
    public void first() {
        System.out.println("######first#####");
        System.out.println("测试1:源Publisher不为空");
        Flowable.just("item2", "item1", "item7").first("item0").subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println("s = " + s);
            }
        });

        System.out.println("测试2:源Publisher为空,first发射默认值");
        Flowable.empty().first("item0").subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                System.out.println("o = " + o.toString());
            }
        });

        System.out.println("测试3:源Publisher为空,发射错误信号");
        Flowable.empty().firstOrError().subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                System.out.println("o = " + o.toString());
            }
        });
    }



测试结果:
######first#####
测试1:源Publisher不为空
s = item2
测试2:源Publisher为空,first发射默认值
o = item0
测试3:源Publisher为空,发射错误信号
java.util.NoSuchElementException

 

 

last

 

1) last作用和使用场景

作用:last操作符和first刚好相反,对对于Single,在源Publisher不为空的情况下返回它发射的项目的最后一项,如果为空则发射指定默认值或者发出NoSuchElementException信号,对于Maybe如果有则发射源Publisher发射的项目的最后一项,没有则直接完成

使用场景:如果只对源Publisher发出的最后一项或满足某些条件的最后一项感兴趣,可以使用last运算符过滤源Publisher。

2) last接口

Single<T>last(T defaultItem)

Returns a Single that emits only the last item emitted by this Flowable, or a default item if this Flowable completes without emitting any items.

返回一个Single,仅发出此Flowable发出的最后一项,如果此Flowable完成而不发出任何项,则返回默认项。

Maybe<T>lastElement()

Returns a Maybe that emits the last item emitted by this Flowable or completes if this Flowable is empty.

返回一个Maybe,它发出此Flowable发出的最后一项,或者如果此Flowable为空则完成。

Single<T>lastOrError()

Returns a Single that emits only the last item emitted by this Flowable or signals a NoSuchElementException if this Flowable is empty.

返回一个Single,它仅发出此Flowable发出的最后一项,或者如果此Flowable为空则发出NoSuchElementException信号。

 

3) last图解和说明

 

4) last测试用例

测试代码

    @Test
    public void last() {
        System.out.println("######last#####");
        System.out.println("测试1:源Publisher不为空");
        Flowable.just("item2", "item1", "item7").last("item0").subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println("s = " + s);
            }
        });

        System.out.println("测试2:源Publisher为空,last");
        Flowable.empty().first("item0").subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                System.out.println("o = " + o.toString());
            }
        });

        System.out.println("测试3:源Publisher为空,发射错误信号");
        Flowable.empty().lastOrError().subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                System.out.println("o = " + o.toString());
            }
        });
    }

测试结果:
######last#####
测试1:源Publisher不为空
s = item7
测试2:源Publisher为空,last
o = item0
测试3:源Publisher为空,发射错误信号java.util.NoSuchElementException

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值