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

Add default method <A> A[] Collection.toArray(IntFunction<A[]> generator)

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: tbd_major
    • Component/s: core-libs
    • Labels:

      Description

      There exists a method on j.u.Stream that makes it a little easier to create an array of an appropriate type (at the expense of throwing a ArrayStoreException if the array component type is not a super type of all elements in the stream):

          <A> A[] toArray(IntFunction<A[]> generator);

      An equivalent method can also be added to Collection.

      Given the existence of:

        Collection.toArray(T[] a)

      this does introduce a source code incompatibility for code passing nulls:

        toArray(null);

      although this is only likely to occur in tests (such as the TCK perhaps).

        Issue Links

          Activity

          Hide
          smarks Stuart Marks added a comment -
          The obvious default implementation would be something like

              return toArray(generator.apply(this.size()));

          however, this would return an array of the wrong size if the collection changes size
          between the calls to size() and toArray(). A safer (though perhaps less efficient)
          default might be

              return toArray(generator.apply(0));
          Show
          smarks Stuart Marks added a comment - The obvious default implementation would be something like     return toArray(generator.apply(this.size())); however, this would return an array of the wrong size if the collection changes size between the calls to size() and toArray(). A safer (though perhaps less efficient) default might be     return toArray(generator.apply(0));
          Hide
          smarks Stuart Marks added a comment -
          I think Paul volunteered you to take this one, Chris. :-)
          Show
          smarks Stuart Marks added a comment - I think Paul volunteered you to take this one, Chris. :-)

            People

            • Assignee:
              chegar Chris Hegarty
              Reporter:
              psandoz Paul Sandoz
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: