Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How method .toArray(IntFunction<A[]> generator) knows the size of new Array [duplicate]

I'm looking for the answer for a question: how method .toArray(IntFunction<A[]> generator) knows the size of new Array.

Actually I know how to use this method to create new Array that contains all of stream elements (e.g. String[]::new, Size -> new String[Size]), but in the original java code we can see that the IntFunction<A[]> generator applies given function to int argument. And there is my question HOW this function gets the number of elements of the stream.

I've been reading the source code of this classes for 3 hours, but I did not find the answer.

like image 238
Martin Avatar asked Oct 20 '25 01:10

Martin


1 Answers

the answer to your question is stated in java docs.

toArray uses the provided generator function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.

The generator function takes an integer, which is the size of the desired array and produces an array of the desired size.

like image 113
Ousmane D. Avatar answered Oct 21 '25 16:10

Ousmane D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!