New convenience methods on Stream

Donald Raab donraab at gmail.com
Tue Apr 27 05:35:59 UTC 2021


I realized after sending that option 2 can be made more abstract:

default <R extends Iterable<T>> R to(Function<T[], R> function)
{
    return function.apply((T[]) this.toArray());
}

> 
> 2. Pass the result of toArray directly into a function that can then return a Collection. This should work with Set.of, List.of and any 3rd party collections which take arrays.
> 
> default <R extends Collection<T>> R to(Function<T[], R> function)
> {
>    return function.apply((T[]) this.toArray());
> }
> 
> Usage Examples:
> 
> Set<String> set = stream.to(Set::of);
> List<String> list = stream.to(List::of);
> List<String> arrayList = stream.to(Arrays::asList);
> 



More information about the core-libs-dev mailing list