RxJava2 Flowable groupBy (变换操作符)

本文详细介绍了RxJava2中的Flowable groupBy操作符,包括其作用、使用场景、接口说明、图解分析以及测试用例,帮助读者理解如何根据特定条件对数据进行分组。

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

groupBy

 

目录

1 groupBy的作用和使用场景

2 groupBy接口

3 groupBy图解和说明

4 groupBy测试用例


1 groupBy的作用和使用场景

根据指定的条件将Publisher发送的数据分组,分组后的数据以key:value的形式被作为Flowable的发射项封装在GroupedFlowable中。

如果遇到需要将数据根据一定条件分类可以使用groupBy操作符

 

2 groupBy接口

<K> Flowable<GroupedFlowable<K,T>>groupBy(Function<? super T,? extends K> keySelector)

Groups the items emitted by a Publisher according to a specified criterion, and emits these grouped items as GroupedFlowables.

根据指定的条件对发布者发出的项目进行分组,并将这些分组的项目作为GroupedFlowables发出。

<K> Flowable<GroupedFlowable<K,T>>groupBy(Function<? super T,? extends K> keySelector, boolean delayError)

Groups the items emitted by a Publisher according to a specified criterion, and emits these grouped items as GroupedFlowables.

根据指定的条件对发布者发出的项目进行分组,并将这些分组的项目作为GroupedFlowables发出。

<K,V> Flowable<GroupedFlowable<K,V>>groupBy(Function<? super T,? extends K> keySelector, Function<? super T,? extends V> valueSelector)

Groups the items emitted by a Publisher according to a specified criterion, and emits these grouped items as GroupedFlowables.

根据指定的条件对发布者发出的项目进行分组,并将这些分组的项目作为GroupedFlowables发出。

<K,V> Flowable<GroupedFlowable<K,V>>groupBy(Function<? super T,? extends K> keySelector, Function<? super T,? extends V> valueSelector, boolean delayError)

Groups the items emitted by a Publisher according to a specified criterion, and emits these grouped items as GroupedFlowables.

根据指定的条件对发布者发出的项目进行分组,并将这些分组的项目作为GroupedFlowables发出。

<K,V> Flowable<GroupedFlowable<K,V>>groupBy(Function<? super T,? extends K> keySelector, Function<? super T,? extends V> valueSelector, boolean delayError, int bufferSize)

Groups the items emitted by a Publisher according to a specified criterion, and emits these grouped items as GroupedFlowables.

根据指定的条件对发布者发出的项目进行分组,并将这些分组的项目作为GroupedFlowables发出。

<K,V> Flowable<GroupedFlowable<K,V>>groupBy(Function<? super T,? extends K> keySelector, Function<? super T,? extends V> valueSelector, boolean delayError, int bufferSize, Function<? super Consumer<Object>,? extends Map<K,Object>> evictingMapFactory)

Groups the items emitted by a Publisher according to a specified criterion, and emits these grouped items as GroupedFlowables.

根据指定的条件对发布者发出的项目进行分组,并将这些分组的项目作为GroupedFlowables发出。

 

3 groupBy图解和说明

图解根据形状进行分组,很形象的说明搞了groupBy的作用

 

 

4 groupBy测试用例

 @Test
    public void groupBy() {
        System.out.println("######groupBy#####");
        Flowable.just(1, 20)
                .groupBy(new Function<Integer, String>() {
                    @Override
                    public String apply(Integer integer) throws Exception {
                        return integer > 10 ? "1组" : "2组";
                    }
                })
                .subscribe(new Consumer<GroupedFlowable<String, Integer>>() {
                    @Override
                    public void accept(GroupedFlowable<String, Integer> groupedFlowable) throws Exception {
                        groupedFlowable.subscribe(new Consumer<Integer>() {
                            @Override
                            public void accept(Integer integer) throws Exception {
                                String key = groupedFlowable.getKey();
                                System.out.println(key + ":" + String.valueOf(integer));
                            }
                        });
                    }
                });

    }


测试结果
######groupBy#####
2组:1
1组:20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值