Issam Bakrim
Issam Bakrim

Reputation: 101

What is the use of count here at the end of stream Java 8

I am a little bit confused about the utility of using count() here in this code :

Stream.iterate(1, (Integer n) -> n + 1)
    .peek(n -> System.out.println("number generated  - " + n))
    .filter(n -> (n % 2 == 0))
    .peek(n -> System.out
    .println("Even number filter passed for  –" + n))
    .limit(5).count();

With count() added to the end of the stream, i get what i want, which is to show this result :

number generated - 1 number generated - 2 Even number filter passed for –2 number generated - 3 number generated - 4 Even number filter passed for –4 number generated - 5 number generated - 6 Even number filter passed for –6 number generated - 7 number generated - 8 Even number filter passed for –8 number generated - 9 number generated - 10 Even number filter passed for –10

but if i delete count() from the end of the stream statement i didn't get any result.

Upvotes: 4

Views: 1131

Answers (3)

Sajit Gupta
Sajit Gupta

Reputation: 108

Stream operations are of two types - intermediate and terminal operations.

Intermediate operations are lazy that means they are not executed on the stream pipeline when invoked but merely add a stage to the pipeline.

The pipeline is executed when a terminal operation is encountered. Thus terminal operations are eager.

Count() being one of the terminal operation executes the stream pipeline and gives the result while on removing it, the pipeline has only intermediate operations and so is not evaluated, hence no result.

Upvotes: 1

Ousmane D.
Ousmane D.

Reputation: 56423

As mentioned by @luk2302 in his answer "you need to perform a terminal operation on the stream for anything to happen".

Just to expand upon this, there is two type of stream operations i.e. an operation is either an intermediate operation which is lazy. lazy meaning as late as possible or executed only when needed; or an operation is eager meaning as soon as possible or immediately.

In the example you've provided, all the operations bar count() are intermediate operations and they will not render the processing of the data unless told otherwise by a terminal operation.

The count() operation is eager (i.e terminal), so it will render the processing of the entire stream.

To determine whether a method in the pipeline is intermediate (lazy) or terminal (eager), you only have to look at what type that methods returns.

The rule is simple. If a method in the stream pipeline returns a stream it’s an intermediate operation therefore lazy. If a method in the stream pipeline returns anything else it’s a terminal operation therefore eager.

Upvotes: 2

luk2302
luk2302

Reputation: 57114

You need to perform a terminal operation on the stream for anything to happen. If you drop the .count() the stream basically does nothing. Think about the method peek and filter and so on to just take in a function and putting it on an internal queue of functions to execute.
The remembered functions will only be executed after you use one terminal operation after which you no longer have a stream but some result or nothing at all. count, collect, reduce, forEach are such terminal operations.

Read more about it in the "Stream operations and pipelines" section of the docs and take a look through the functions in the Stream docs to see which methods are terminal and which are not.

Upvotes: 4

Related Questions