Rxjava:take相关方法的使用

在这里插入图片描述

从一个数据源中,获取符合条件的数据

take(final int count):获取指定个数

public final Observable<T> take(final int count) 

截取发射源的count个数据,如果发射源的数据数量小于count,则获取全部数据。

测试:

@Test
public void testTake(){

    List list = new ArrayList();
    for (int i = 0; i < 10; i++) {
        list.add(i);
    }

    Observable.from(list).take(3).subscribe(new Action1() {
        @Override
        public void call(Object o) {
            System.out.println("o = " + o);
        }
    });

}

输出:

o = 0
o = 1
o = 2

take(long time, TimeUnit unit):获取指定时间片段内的数据

public final Observable<T> take(long time, TimeUnit unit)

除了按照数量获取,还可以根据时间段获取。

测试:

@Test
public void testTakeByTime() {
	//每1秒发射一个数据
    Observable.interval(0,1, TimeUnit.SECONDS)
    //获取3秒内发射的数据
            .take(3, TimeUnit.SECONDS)
            .subscribe(new Action1<Long>() {
                @Override
                public void call(Long aLong) {
                    System.out.println("aLong = " + aLong);
                }
            });

    for (;;){
    }

}

输出:

aLong = 0
aLong = 1
aLong = 2

takeLast:获取最后发射的指定个数的数据

public final Observable<T> takeLast(final int count) 

测试:

        Observable.just(1,2,3,4,5).takeLast(2).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                System.out.println("integer = " + integer);
            }
        });

输出:

integer = 4
integer = 5

takeLast与take

到这里会产生一个疑问,take(final int count)方法从数据开始发射时计数,takeLast应该是在数据发射结束时开始计数,为了验证想法,我们每1秒发射一个数据,并分别使用take(final int count)和takeLast进行测试:

take(final int count)

@Test
public void testTakeInterval(){
    Observable.interval(0,1,TimeUnit.SECONDS)
            .doOnNext(new Action1<Long>() {
                @Override
                public void call(Long aLong) {
                    System.out.println("TakeTest.call");
                }
            }).take(5).subscribe(new Action1<Long>() {
        @Override
        public void call(Long aLong) {
            System.out.println("aLong = " + aLong);
        }
    });

    for (;;){

    }
}

输出:

TakeTest.call
aLong = 0
TakeTest.call
aLong = 1
TakeTest.call
aLong = 2
TakeTest.call
aLong = 3
TakeTest.call
aLong = 4

takeLast

@Test
public void testTakeLast(){

     Observable.interval(0,1,TimeUnit.SECONDS)
             .doOnNext(new Action1<Long>() {
                 @Override
                 public void call(Long aLong) {
                     System.out.println("TakeTest.call");
                 }
             }).take(5)
             .takeLast(3).subscribe(new Action1<Long>() {
         @Override
         public void call(Long aLong) {
             System.out.println("aLong = " + aLong);
         }
     });

     for (;;){

     }
 }

输出:

TakeTest.call
TakeTest.call
TakeTest.call
TakeTest.call
TakeTest.call
aLong = 2
aLong = 3
aLong = 4

对比输出结果,take每收到一次数据就立刻发射出去,而takeLast是把数据缓存下来,等数据发射结束,一起打包发射。

和take类似,takeLast也有按照时间片段获取数据的重载方法,这里不再赘述。

takeLastBuffer:把数据组合成List统一发射。

前面通过takeLast了解到,数据会在数据源停止发射时,一起发射缓存的数据,takeLastBuffer则是将这些数据组合成List发射,重载方法与take类似。

测试:

@Test
public void testTakeLastBuffer() {
    Observable.just(1, 2, 3, 4, 5)
            .takeLastBuffer(3).subscribe(new Action1<List<Integer>>() {
        @Override
        public void call(List<Integer> integers) {
            System.out.println("integers = " + integers);
        }
    });
}

输出:

integers = [3, 4, 5]

takeFirst:获取第一个符合条件的数据

public final Observable<T> takeFirst(Func1<? super T, Boolean> predicate)

获取大于3的第一个数据,测试:

@Test
public void testTakeFirst(){
    Observable.just(1,2,3,4,5).takeFirst(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer integer) {
            return integer > 3;
        }
    }).subscribe(new Action1<Integer>() {
        @Override
        public void call(Integer integer) {
            System.out.println("integer = " + integer);
        }
    });
}

输出:

integer = 4

takeWhile:获取符合条件的数据

public final Observable<T> takeWhile(final Func1<? super T, Boolean> predicate)

测试:

@Test
public void testTakeWhile(){
    Observable.just(1,2,3,4,5)
            .takeWhile(new Func1<Integer, Boolean>() {
                @Override
                public Boolean call(Integer integer) {
                    return integer < 3;
                }
            }).subscribe(new Action1<Integer>() {
        @Override
        public void call(Integer integer) {
            System.out.println("integer = " + integer);
        }
    });

    for (;;);
}

输出:

integer = 1
integer = 2

takeUntil:

public final <E> Observable<T> takeUntil(Observable<? extends E> other)

第一个数据源发射数据,当第二个数据源开始发射数据时,停止发射数据。

测试:

@Test
public void testTakeUntil(){
    Observable<Long> observable = Observable.interval(3, 1, TimeUnit.SECONDS);
    Observable.interval(0,1,TimeUnit.SECONDS).takeUntil(observable).subscribe(new Action1<Long>() {
        @Override
        public void call(Long aLong) {
            System.out.println("aLong = " + aLong);
        }
    });
    for (;;);
}

输出:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值