collect()
When none of the collection operators can do what you need, you can always use the collect() operator to specify a different type to collect items into.
For instance, there is no toSet() operator to collect emissions in a Set<T>, but you can quickly use collect(Callable<U> initialValueSupplier, BiConsumer<U,T> collector) to effectively do this.
Let's say you need to collect String values in a Set<String> implementation. To accomplish that, you can specify the first argument—the function that produces an initial value of the Set<String> implementation you would like to use, and the second argument—the function that is going to collect the values (whatever you need to collect) in that Set<String> implementation you have chosen. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access