Java 8 Streams peek api -


i tried following snippet of java 8 code peek.

list<string> list = arrays.aslist("bender", "fry", "leela"); list.stream().peek(system.out::println); 

however there nothing printed out on console. if instead:

list.stream().peek(system.out::println).foreach(system.out::println); 

i see following outputs both peek foreach invocation.

bender bender fry fry leela leela 

both foreach , peek take in (consumer<? super t> action) why output different?

the javadoc mentions following:

intermediate operations return new stream. lazy; executing intermediate operation such filter() not perform filtering, instead creates new stream that, when traversed, contains elements of initial stream match given predicate. traversal of pipeline source not begin until terminal operation of pipeline executed.

peek being intermediate operation nothing. on applying terminal operation foreach, results printed out seen.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -