Rx java 使用RxJava计算百分比份额

Rx java 使用RxJava计算百分比份额,rx-java,Rx Java,假设我有以下数据: Map<String, Integer> counts = new HashMap<>(); counts.put("one", 1); counts.put("two", 2); counts.put("three", 3); 如何使用RxJava高效地计算每个类别的百分比份额 请注意,这是一个人为的例子,在这个例子中,重新处理源数据(实际上已经作为可观察数据出现在我的脑海中)并不是一件小事 非常感谢您能提供的任何帮助, 丹 到目前为止我尝试过的事

假设我有以下数据:

Map<String, Integer> counts = new HashMap<>();
counts.put("one", 1);
counts.put("two", 2);
counts.put("three", 3);
如何使用RxJava高效地计算每个类别的百分比份额

请注意,这是一个人为的例子,在这个例子中,重新处理源数据(实际上已经作为可观察数据出现在我的脑海中)并不是一件小事

非常感谢您能提供的任何帮助, 丹

到目前为止我尝试过的事情


我遇到的问题是,对于我的观测值集,我总是需要在合并各个类别以计算总数后返回并重新处理它们。为此,我尝试发布和使用主题,但在每种情况下,在计算百分比之前都需要计算总数(即阻塞),这意味着我的.connect()或onNext()永远不会运行。

下面的代码获取流{“一”、“二”、“一”}并输出:

WithPercent [value=one, percent=66.66666666666667]
WithPercent [value=two, percent=33.333333333333336]
可运行类:

import rx.Observable;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        Observable.just("one", "two", "one")
                .groupBy(x -> x)
                .flatMap(g -> g.count()
                        .map(n -> new WithCount<String>(g.getKey(), n)))
                // now get a list of the counts by key
                .toList()
                // iterate the list and get totals, calculate percents and emit
                .flatMap(list -> {
                    int total = list.stream().mapToInt(wc -> wc.count).sum();
                    return Observable.from(list).map(
                            wc -> new WithPercent<String>(wc.value, 100.0 * wc.count / total));
                }).subscribe(System.out::println);
    }

    private static class WithCount<T> {
        final T value;
        final int count;

        WithCount(T value, int count) {
            this.value = value;
            this.count = count;
        }
    }

    private static class WithPercent<T> {
        final T value;
        final double percent;

        WithPercent(T value, double percent) {
            this.value = value;
            this.percent = percent;
        }

        @Override
        public String toString() {
            return "WithPercent [value=" + value + ", percent=" + percent + "]";
        }

    }
}
导入接收。可观察;
公共班机{
公共静态void main(字符串[]args)引发InterruptedException{
可观察。只是(“一”、“二”、“一”)
.groupBy(x->x)
.flatMap(g->g.count()
.map(n->newwithcount(g.getKey(),n)))
//现在按键获取计数列表
托利斯先生()
//迭代列表并获取总数,计算百分比并发射
.flatMap(列表->{
int total=list.stream().mapToInt(wc->wc.count.sum();
返回可观察的。来自(列表)。映射(
wc->new WithPercent(wc.value,100.0*wc.count/total));
}).subscribe(System.out::println);
}
私有静态类WithCount{
最终T值;
最终整数计数;
WithCount(T值,int计数){
这个值=值;
this.count=计数;
}
}
私有静态类的百分比{
最终T值;
最后的两个百分点;
带百分比(T值,双百分比){
这个值=值;
这个百分比=百分比;
}
@凌驾
公共字符串toString(){
返回“WithPercent[value=“+value+”,percent=“+percent+””;
}
}
}

下面的代码获取流{“一”、“二”、“一”}并输出:

WithPercent [value=one, percent=66.66666666666667]
WithPercent [value=two, percent=33.333333333333336]
可运行类:

import rx.Observable;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        Observable.just("one", "two", "one")
                .groupBy(x -> x)
                .flatMap(g -> g.count()
                        .map(n -> new WithCount<String>(g.getKey(), n)))
                // now get a list of the counts by key
                .toList()
                // iterate the list and get totals, calculate percents and emit
                .flatMap(list -> {
                    int total = list.stream().mapToInt(wc -> wc.count).sum();
                    return Observable.from(list).map(
                            wc -> new WithPercent<String>(wc.value, 100.0 * wc.count / total));
                }).subscribe(System.out::println);
    }

    private static class WithCount<T> {
        final T value;
        final int count;

        WithCount(T value, int count) {
            this.value = value;
            this.count = count;
        }
    }

    private static class WithPercent<T> {
        final T value;
        final double percent;

        WithPercent(T value, double percent) {
            this.value = value;
            this.percent = percent;
        }

        @Override
        public String toString() {
            return "WithPercent [value=" + value + ", percent=" + percent + "]";
        }

    }
}
导入接收。可观察;
公共班机{
公共静态void main(字符串[]args)引发InterruptedException{
可观察。只是(“一”、“二”、“一”)
.groupBy(x->x)
.flatMap(g->g.count()
.map(n->newwithcount(g.getKey(),n)))
//现在按键获取计数列表
托利斯先生()
//迭代列表并获取总数,计算百分比并发射
.flatMap(列表->{
int total=list.stream().mapToInt(wc->wc.count.sum();
返回可观察的。来自(列表)。映射(
wc->new WithPercent(wc.value,100.0*wc.count/total));
}).subscribe(System.out::println);
}
私有静态类WithCount{
最终T值;
最终整数计数;
WithCount(T值,int计数){
这个值=值;
this.count=计数;
}
}
私有静态类的百分比{
最终T值;
最后的两个百分点;
带百分比(T值,双百分比){
这个值=值;
这个百分比=百分比;
}
@凌驾
公共字符串toString(){
返回“WithPercent[value=“+value+”,percent=“+percent+””;
}
}
}

下面的代码获取流{“一”、“二”、“一”}并输出:

WithPercent [value=one, percent=66.66666666666667]
WithPercent [value=two, percent=33.333333333333336]
可运行类:

import rx.Observable;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        Observable.just("one", "two", "one")
                .groupBy(x -> x)
                .flatMap(g -> g.count()
                        .map(n -> new WithCount<String>(g.getKey(), n)))
                // now get a list of the counts by key
                .toList()
                // iterate the list and get totals, calculate percents and emit
                .flatMap(list -> {
                    int total = list.stream().mapToInt(wc -> wc.count).sum();
                    return Observable.from(list).map(
                            wc -> new WithPercent<String>(wc.value, 100.0 * wc.count / total));
                }).subscribe(System.out::println);
    }

    private static class WithCount<T> {
        final T value;
        final int count;

        WithCount(T value, int count) {
            this.value = value;
            this.count = count;
        }
    }

    private static class WithPercent<T> {
        final T value;
        final double percent;

        WithPercent(T value, double percent) {
            this.value = value;
            this.percent = percent;
        }

        @Override
        public String toString() {
            return "WithPercent [value=" + value + ", percent=" + percent + "]";
        }

    }
}
导入接收。可观察;
公共班机{
公共静态void main(字符串[]args)引发InterruptedException{
可观察。只是(“一”、“二”、“一”)
.groupBy(x->x)
.flatMap(g->g.count()
.map(n->newwithcount(g.getKey(),n)))
//现在按键获取计数列表
托利斯先生()
//迭代列表并获取总数,计算百分比并发射
.flatMap(列表->{
int total=list.stream().mapToInt(wc->wc.count.sum();
返回可观察的。来自(列表)。映射(
wc->new WithPercent(wc.value,100.0*wc.count/total));
}).subscribe(System.out::println);
}
私有静态类WithCount{
最终T值;
最终整数计数;
WithCount(T值,int计数){
这个值=值;
this.count=计数;
}
}
私有静态类的百分比{
最终T值;
最后的两个百分点;
带百分比(T值,双百分比){
这个值=值;
这个百分比=百分比;
}
@凌驾
公共字符串toString(){
返回“WithPercent[value=“+value+”,percent=“+percent+””;
}
}
}

下面的代码获取流{“一”、“二”、“一”}并输出:

WithPercent [value=one, percent=66.66666666666667]
WithPercent [value=two, percent=33.333333333333336]
可运行类:

import rx.Observable;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        Observable.just("one", "two", "one")
                .groupBy(x -> x)
                .flatMap(g -> g.count()
                        .map(n -> new WithCount<String>(g.getKey(), n)))
                // now get a list of the counts by key
                .toList()
                // iterate the list and get totals, calculate percents and emit
                .flatMap(list -> {
                    int total = list.stream().mapToInt(wc -> wc.count).sum();
                    return Observable.from(list).map(
                            wc -> new WithPercent<String>(wc.value, 100.0 * wc.count / total));
                }).subscribe(System.out::println);
    }

    private static class WithCount<T> {
        final T value;
        final int count;

        WithCount(T value, int count) {
            this.value = value;
            this.count = count;
        }
    }

    private static class WithPercent<T> {
        final T value;
        final double percent;

        WithPercent(T value, double percent) {
            this.value = value;
            this.percent = percent;
        }

        @Override
        public String toString() {
            return "WithPercent [value=" + value + ", percent=" + percent + "]";
        }

    }
}
导入接收。可观察;
公共班机{
公共静态void main(字符串[]args)引发InterruptedException{
可观察。只是(“一”、“二”、“一”)
.groupBy(x->x)
.flatMap(g->g.count()
.map(n->newwithcount(g.getKey(),n)))
//现在按键获取计数列表
托利斯先生()
//迭代列表并获取总数,计算百分比并发射
.flatMap(列表->{
int total=list.stream().mapToInt(wc->wc.count.sum();
返回可观察的。来自(列表)。映射(
wc->new WithPercent(wc.value,100.0*wc.count/total));