RxJava或F#函数[List <string> to Map <integer,string =“”>]其中Integer是String更改的位置

时间:2016-12-10 14:54:40

标签: android functional-programming rx-java

我正在尝试从输入列表中提取标题的Map(标题的位置+标题中的文本)。我已将输入映射到它们所属的标题(或组)。我需要一个或多或少像distinctdistinctUntilChanged的函数,但我可以获得文本更改的位置。

到目前为止,我有这个:

Observable.from(inputs)
            .map(this::getSectionTitle) // Maps the inputs into the Header string
            .distinct();

显然会返回标题列表。如果不清楚,我需要链接到它们应放置位置的标题列表。

或者我也可以接受F#类型List<String> -> Set<(Int*String)>中的函数。

修改

这是我想要从impartive转变为functionl方法的函数:

for (int i = 0; i < inputs.size(); i++) {
        Input pu = inputs.get(i);
        String header = getSectionTitle(pu);
        if (!mHeaders.containsValue(header)) {
            mHeaders.put(i + mHeaders.size(), header);
        }
    }

编辑2:

输入/输出的示例

["a","a","a","b","b","c"] -> [("a",0),("b",3),("c",5)]

1 个答案:

答案 0 :(得分:1)

你可以使用这样的东西

 Observable.from(inputs)
        .map(this::getSectionTitle) // Maps the inputs into the Header string
        .zipWith(Observable.range(0, 1000), Pair::create) // This is a weak point of the solution as you should know max number of emissions as if it exceeds the number in range, it will stop emitting .. 
        .distinctUntilChanged(pair -> pair.first) // keySelector function tells the operator what is the value object upon which it should determine whether it was changed
        .map(pair -> /* Map to your desired format */)

该对对应于已更改的项目和发出它的订单号...

编辑 :您可以使用带扫描运算符的计时器代替Observable.range()。您不应该依赖于计时器可观察到的直接发射,因为不能保证它是一个连续序列,因此是一个扫描操作符。

Observable.interval(1, TimeUnit.MILLISECONDS)
    .scan(0L, (index, emittedValue) -> index++)