0

I want to build a async operation that iterates chars in given string. I have a char array taken by "mystring".toCharArray(). I want to iterate each 10th character by using RX.

I know i can do it with AsyncTask and for-loops but i thought RX would be more elegant solution. I have read documentations but did not recognize how to do it.

Another idea in my mind to create a PublishSubject and fire onNext() in a for-loop that index increments by 10 with subscription.

PS: "mystring" can be much more larger like a json, xml or etc. Please feel free to comment about ram profiling.

1 Answer 1

3

RxJava doesn't support primitive arrays, but you can use a for loop in the form of the range operator and index into a primitive array. With some index arithmetic, you can get every 10th character easily:

char[] chars = ...

Observable.range(0, chars.length)
    .filter(idx -> idx % 10 == 0)
    .map(idx -> chars[idx])
    .subscribe(System.out::println);
Sign up to request clarification or add additional context in comments.

4 Comments

I got it, Something is niggling my mind. With PublishSubject i can iterate less which makes it faster?
Depends on what you have written and how do you measure it as "faster".
Doing for-loops in Java and not in RxJava has always lower overhead. I use a scrabble benchmark to measure stream processing libraries and you can get as 4x more overhead with RxJava compared to a plain for loop when everything is synchronous.
You are right. I also wants to write a code that easy to read. Its a trade-off. I will stick with range. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.