API question/request: Array to Stream
Paul Sandoz
paul.sandoz at oracle.com
Tue Oct 1 07:39:53 PDT 2013
On Oct 1, 2013, at 4:29 PM, Per Bothner <per at bothner.com> wrote:
> On 10/01/2013 07:16 AM, Paul Sandoz wrote:
>>
>> On Oct 1, 2013, at 3:24 PM, "Millies, Sebastian" <Sebastian.Millies at softwareag.com> wrote:
>>
>>> Why can't I stream an array? It is awkward having to convert to a list first, cf.
>>> this code by Venkat Subramaniam:
>
> On 10/01/2013 07:16 AM, Paul Sandoz wrote
>> Use:
>>
>> Stream.of(filters)...
>
> Better, IMO: Arrays.stream(filters), for 2 reasons:
>
> (1) Arrays.stream has options startInclusive/endInclusive bounds.
> If you have an array buffer that's not necessary full, then you
> need the endInclusive (though admittedly an alternative is Stream#limit).
>
Yes, if you need to stream over a range elements in the array. (While there is limit i would not recommend using that if one can effectively limit at the source since that will be more efficient.)
Otherwise, i prefer Stream.of. As you say it is a matter of style so somewhat subjective.
Paul.
> (2) As a matter of style, Stream#of is a varargs method: It takes a
> variable number of T, not an array of T. True, variable number of T
> is implemented as (and effectively overloaded as) an array of T,
> but Java varargs is a bit of a hack and and using ivarargs to pass a
> single array is less readable I think.
> --
> --Per Bothner
> per at bothner.com http://per.bothner.com/
More information about the lambda-dev
mailing list