-
Enhancement
-
Resolution: Unresolved
-
P4
-
8
A DESCRIPTION OF THE REQUEST :
Please add a method similar to the following to the java.util.stream.Stream.Builder<T> class:
default Builder<T> addAll(Iterable<? extends T> iterable) {
for (T t : iterable) {
accept(t);
}
return this;
}
JUSTIFICATION :
I can currently create a Stream via the builder interface, such as:
Stream<String> foo = Stream.builder().add("A").add("B").add("C").build();
However, if I wish to add one or more existing collections to the stream, I have no easy way to do it, particularly using chained function calls. I would like to be able to do this:
List<String> list = new HashSet<>(Arrays.asList("B", "C", "D");
Stream<String> foo = Stream.builder().add("A").addAll(list).add("E").build();
Note that Google Guava has similar features for its builders which are used when building immutable collections.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A Stream.Builder.addAll method as I described above.
ACTUAL -
No such method exists.
CUSTOMER SUBMITTED WORKAROUND :
Don't use chained calls to build streams which need to contain contents of collections. Break the statement into multiple statements, like so.
List<String> list = new HashSet<>(Arrays.asList("B", "C", "D");
Stream.Builder<String> builder = Stream.builder().add("A");
list.forEach(builder::add);
Stream<String> foo = builder.add("E").build();
Please add a method similar to the following to the java.util.stream.Stream.Builder<T> class:
default Builder<T> addAll(Iterable<? extends T> iterable) {
for (T t : iterable) {
accept(t);
}
return this;
}
JUSTIFICATION :
I can currently create a Stream via the builder interface, such as:
Stream<String> foo = Stream.builder().add("A").add("B").add("C").build();
However, if I wish to add one or more existing collections to the stream, I have no easy way to do it, particularly using chained function calls. I would like to be able to do this:
List<String> list = new HashSet<>(Arrays.asList("B", "C", "D");
Stream<String> foo = Stream.builder().add("A").addAll(list).add("E").build();
Note that Google Guava has similar features for its builders which are used when building immutable collections.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A Stream.Builder.addAll method as I described above.
ACTUAL -
No such method exists.
CUSTOMER SUBMITTED WORKAROUND :
Don't use chained calls to build streams which need to contain contents of collections. Break the statement into multiple statements, like so.
List<String> list = new HashSet<>(Arrays.asList("B", "C", "D");
Stream.Builder<String> builder = Stream.builder().add("A");
list.forEach(builder::add);
Stream<String> foo = builder.add("E").build();