Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8058705

Please add java.util.stream.Stream.Builder<T>.addAll(Iterable<? extends T>)

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8
    • core-libs

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



            psandoz Paul Sandoz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: