Quickie: JAVA 8 Stream API: Stream Initialization

Stream represents a sequence of values. Stream API, introduced in JAVA 8 (JSR-355 [1]) enables declarative specification of computation on data. It may also interesting to note that it imposes no restriction on the data source.

Following are the important facts:

  • Stream != Collection
    • It is bound to a data source which can be {collection, array, IO, iterator}
  • Stream does not hold any data
    • It is simply an intermediate data
  • Stream can’t modify its source
    • Good for parallelism.
  • Stream can be infinite.

In this post, we only discuss how to construct a Stream object.

A Stream object can be constructed in the following manner.

    Collection source = /*a collection that will be used as a source of Stream*/    

    source.stream(); 

Alternatively, Stream contains several builders, as listed below.

  • IntStream: Stream.of(1,3,4)
  • EmptyStream: Stream.empty()
  • InfiniteStream: Stream.iterate(1, n-> n+10)

In the next post, we shall discuss more on how we can use a Stream to specify data transformation in a declarative manner.

References:

[1] JSR-355: Lambda Expression for Java.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s