0

I am trying to collect some property from list of objects into array using RxJava. I've wrote the code, but now I can't cast return type properly. So, I've tried this code:

Observable.just(rangeList)
            .flatMap(Observable::from)
            .map((range) -> String.format(Locale.getDefault(), "%s (%d)", range, range.getElementNumber()))
            .cast(String.class)
            .toList()
            .toBlocking().first()
            .toArray();

But it still returns Object[], and I need String[].

dbulgakov
  • 81
  • 7

1 Answers1

2

You could use the .toArray() parameter:

.toArray(new String[length]);

It is well described in the documentation:

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

TheRealVira
  • 1,444
  • 4
  • 16
  • 28