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);
>