New convenience methods on Stream
Donald Raab
donraab at gmail.com
Wed Apr 28 22:58:05 UTC 2021
I looked through a few libraries and found some methods where the option #2 proposal for Steam might be useful. If the JDK had constructors for ArrayList, HashSet and other collection types that take arrays this method would work there as well.
> default <R extends Iterable<T>> R to(Function<T[], R> function)
> {
> return function.apply((T[]) this.toArray());
> }
// JDK
Set<String> set = stream.to(Set::of);
List<String> list = stream.to(List::of);
List<String> arraysAsList = stream.to(Arrays::asList);
// Guava
ArrayList<String> arrayList = stream.to(Lists::newArrayList);
HashSet<String> hashSet = stream.to(Sets::newHashSet);
Multiset<String> multiset = stream.to(ImmutableMultiset::copyOf);
List<String> guavaList = stream.to(ImmutableList::copyOf);
Set<String> guavaSet = stream.to(ImmutableSet::copyOf);
// Apache Commons Collections
FluentIterable<String> fluentIterable = stream.to(FluentIterable::of);
// Eclipse Collections
MutableList<String> adapter = stream.to(ArrayAdapter::adapt);
MutableList<String> mutableList = stream.to(Lists.mutable::with);
MutableSet<String> mutableSet = stream.to(Sets.mutable::with);
MutableBag<String> mutableBag = stream.to(Bags.mutable::with);
// Eclipse Collections - ListIterable, SetIterable and Bag all extend Iterable, not Collection
ListIterable<String> listIterable = stream.to(Lists.mutable::with);
SetIterable<String> setIterable = stream.to(Sets.mutable::with);
Bag<String> bag = stream.to(Bags.mutable::with);
// Eclipse Collections - Immutable Collections do not extend Collection
ImmutableList<String> immutableList = stream.to(Lists.immutable::with);
ImmutableSet<String> immutableSet = stream.to(Sets.immutable::with);
ImmutableBag<String> immutableBag = stream.to(Bags.immutable::with);
// Eclipse Collections - Stack does not extend Collection
StackIterable<String> stackIterable = stream.to(Stacks.mutable::with);
MutableStack<String> mutableStack = stream.to(Stacks.mutable::with);
ImmutableStack<String> immutableStack = stream.to(Stacks.immutable::with);
// Eclipse Collections - Mutable Map and MutableBiMap are both Iterable<V> so they are valid returns
MutableMap<String, String> map =
stream.to(array -> ArrayAdapter.adapt(array)
.toMap(String::toLowerCase, String::toUpperCase));
MutableBiMap<String, String> biMap =
stream.to(array -> ArrayAdapter.adapt(array)
.toBiMap(String::toLowerCase, String::toUpperCase));
Thanks,
Don
> On Apr 27, 2021, at 1:35 AM, Donald Raab <donraab at gmail.com> wrote:
>
> 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