API question/request: Array to Stream
Samir Talwar
samir at noodlesandwich.com
Wed Oct 2 03:50:25 PDT 2013
I don't think this is much of an issue. If you put the generics in as you
*expect* the program to work (i.e. use Stream<Integer> wherever you have a
raw Stream), your program will fail to compile on that last one. All IDEs
warn you when you don't have generics, and I'd expect anyone using Java 8
APIs to be comfortable using them.
— Samir.
On Wed, Oct 2, 2013 at 11:45 AM, Arne Siegel
<v.a.ammodytes at googlemail.com>wrote:
> I think the current API decisions regarding stream creation from arrays are
> a bit dangerous. Look at this example, based on a theme from Josh Blochs
> Effective Java:
>
> IntStream is1 = IntStream.of(1,2,3,5);
> System.out.println("Number of elements in stream: " +
> is1.count()); // --> 4
>
> Stream is2 = Stream.of(1,2,3,5);
> System.out.println("Number of elements in stream: " +
> is2.count()); // --> 4
>
> int[] data = {6,7,8,7};
>
> IntStream is3 = Arrays.stream(data);
> System.out.println("Number of elements in stream: " +
> is3.count()); // --> 4
>
> IntStream is4 = IntStream.of(data);
> System.out.println("Number of elements in stream: " +
> is4.count()); // --> 4
>
> Stream is5 = Stream.of(data);
> System.out.println("Number of elements in stream: " +
> is5.count()); // --> 1 !!!!
>
>
>
>
> 2013/10/2 Remi Forax <forax at univ-mlv.fr>
>
> > On 10/02/2013 11:02 AM, Paul Sandoz wrote:
> > > On Oct 2, 2013, at 10:07 AM, Gernot Neppert <mcnepp02 at googlemail.com>
> > wrote:
> > >
> > >> Oops, I hadn't noticed there were 2 factory methods at different
> places
> > >> doing exactly the same thing.
> > >> Won't this perpetually provoke the question "Which one should I use?"
> > >>
> > > In this case i think it is mostly harmless; the answer being "which
> ever
> > one you prefer" (they are effectively the same thing).
> >
> > And it's not the first time that we have the same method at two places,
> > by example Integer.toString(int) and String.valueOf(int).
> > if one call the other, as Paul said, it's more to avoid a discovery
> > problem.
> >
> > >
> > >
> > >> Since java.util.Arrays has such a complete set of Stream-creation
> > methods,
> > >> maybe one could drop Stream.of(T...) in favour of Arrays.stream(T...)?
> > >>
> > > The problem with Arrays, as with other factory creation methods, is it
> > can be often hard to find things.
> > >
> > > So we have placed a few static factory methods on Stream itself, and
> > Stream.of serves the duel purpose of creating a stream from an explicit
> > enumeration of elements or from an array of elements.
> > >
> > > Note that we have avoided adding List.of etc (as proposed by Stephen)
> > due to time constraints but also because it is likely we can do better
> with
> > collection literals.
> >
> > and Collection literals was one item of project coin that was delayed
> > and moved from jdk7 to jdk8 :)
> >
> > >
> > > Paul.
> >
> > Rémi
> >
> >
> >
>
>
More information about the lambda-dev
mailing list