How to use Streams api peek() function and make it work?


According to <a href="https://stackoverflow.com/questions/29586014/java-8-streams-peek-api" rel="nofollow">this question</a>, peek() is lazy it means we should somehow activate it. In fact, to activate it to print something out to the console I tried this :

Stream<String> ss = Stream.of("Hi","Hello","Halo","Hacker News"); ss.parallel().peek(System.out::println); System.out.println("lol"); // I wrote this line to print sth out to terminal to wake peek method up

But that doesn't work and the output is :


Thus, how can I make the peek function actually work?

If there is no way to that so whats the point of using peek?


You have to use <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps" rel="nofollow">terminal operation</a> on a stream for it to execute (peek is not terminal, it is an intermediate operation, that returns a new Stream), e.g. count():

Stream<String> ss = Stream.of("Hi","Hello","Halo","Hacker News"); ss.parallel().peek(System.out::println).count();

Or replace peek with forEach (which is terminal):



peek() method uses Consumer as parameter which means that <strong>potentially you can mutate the state of the incoming element</strong>. <strong>At the same time Java documentation says that peek should be mostly used for debugging purposes.</strong> It is an intermediate operator and requires a terminal operator like forEach.



  • MapPageRoute Breaks ActionLinks in Integrated MVC/WebForms App
  • Vpn client in xamarin
  • Vue中实现token验证
  • dijit.byId doesn't work on IE8 with dojo.addOnLoad
  • Is it possible to dynamically import modules?
  • How to get next case of enum(i.e. write a circulating method) in Swift 4.2
  • Swift UIApplication.setStatusBarStyle Doesn't work
  • Android: Get localized permission group names
  • Is using batch size as 'powers of 2' is faster on tensorflow?
  • ggplotly text aesthetic causing geom_line to not display
  • Can't copy/move files with space at end of file name
  • Video file is stuck at 0% percent when being uploaded via YouTube API
  • Uploading new images to Azure FileService using 'CREATEFILE' service
  • Do the different methods of creating classes in Javascript have names and what are they?
  • jQuery Waypoints - multiple divs with same CLASS
  • how to clean RAM programmatically cocoa?
  • unable to set Row.Readonly=false in Datagridview in winforms
  • WPF C# Get Root Node of TreeView
  • Rename Bootstrap Tab with jQuery
  • WooCommerce Free Shipping - Remove raw or change the text name on checkout and email
  • Missing App Store icon codename one build
  • NextJS auth with an external server
  • Forward slash vs backward slash for file path in git bash
  • How do methods within a class access class constant variables?
  • Generate a runnable jar and include libraries in it with Maven
  • Converting bitmap to byte and vice versa
  • Creating http response as a return value for mockito
  • Python ctypes: Prototype with LPCSTR [out] parameter
  • Signed Java web start application with Glassfish 4.1 and Java7
  • Spotify cocoalibspotify offline status set to 1 but all tracks stuck at waiting
  • JavaMail connection problems [duplicate]
  • Autocomplete source from project settings
  • Stop an element moving with padding on hover
  • Neo4j…how to get a visual representation of my data?
  • Python 3x- Compression Makes File Bigger :(
  • How to specify generic type when the type is only known at runtime?
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Android Library Projects on Windows and Mac