I have the following use case (this is of course a contrived example, but once I know the answer, I will be able to port it to a real problem I am to solve):
- Get a list of integers.
- Group them by the result of the % 4 operation
- Collect the each group's elements to lists
- Ignore any groups/lists which have fewer elements than 3 elements
- Emit a single list, whose elements are the lists created in step #3
Here is my current code:
Observable
.from(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12))
.groupBy(item -> item % 4)
.subscribe(groupedObservable -> {
groupedObservable
.toList()
.filter(list -> list.size() > 2)
.subscribe(result -> {
System.out.printf("%d results %s%n", result.size(), result);
});
});
and its output is:
4 results [0, 4, 8, 12]
3 results [2, 6, 10]
3 results [3, 7, 11]
So it prints out how many elements each group has and then the list of elements. I would like the output to be (I actually don't care about the keys):
3 results: [[0, 4, 8, 12], [2, 6, 10], [3, 7, 11]]
i.e. somehow flatten the grouped observables into one list. I fail to do so. For example, adding .flatMap(integers -> Observable.just(integers))
after the filter
doesn't change anything, as it just influences each grouped observable, not the whole stream. Is there a way to fulfill my requirements?
I'm not sure if I've understood you correctly, but based on the desired output here is the code you might be looking for: