How To Debug Coffee Viii Stream Pipeline - Peek() Method Event Tutorial
I create got been writing well-nigh around of import methods from Java SE 8 like map(), flatMap(), collect() etc from quite around fourth dimension in addition to today I'll portion my sense well-nigh around other useful method peek() from java.utill.stream.Stream class. The peek() method of Stream storey tin live on really useful to debug in addition to empathize streams inwards Java 8. You tin exercise the peek() method to run across the elements equally they catamenia from i measuring to around other similar when you lot exercise the filter() method for filtering, you lot tin genuinely run across how filtering is working e.g. lazy evaluation equally good equally which elements are filtered. the peek() method returns a current consisting of the elements of this current in addition to performs the activity requested past times the client.
The peek() method await a Consumer functional interface to perform a non-interfering activity on the elements of this stream, ordinarily printing them using forEach() method.
Btw, the sole argue of using peek() is debugging Stream pipeline, fifty-fifty the API itself says that peek() is exclusively at that topographic point for debugging, but it does aid to empathize the lazy evaluation technique Stream uses to improve performance.
Lazy evaluation agency zilch is evaluated inwards the Stream until a final method similar forEach(), collect() or reduce() is called in addition to processing stops equally presently equally the number is obtained, which agency non all the elements of Stream is processed always.
It all depends upon what form of number you lot desire from Stream. For example, if you lot telephone call upwards findFirst() method so equally presently equally it finds the commencement chemical cistron fulling the criterion, processing stops. If you lot desire to empathize lazy evaluation inwards depth, you lot tin too cheque a comprehensive Java course of pedagogy like The Complete Java MasterClass or a specialized Java 8 books like Java 8 inwards Action by Manning publications.
This is a really mutual code inwards Java 8 in addition to volition aid you lot to larn how current pipeline processing plant inwards Java 8? What happens inwards each step? What is the output or information inwards the current later each measuring etc?
Consider the next example, which calls the peek() method later each measuring inwards a Stream pipeline involving filter() and map() methods:
In this example, nosotros create got a Stream of String in addition to so nosotros are filtering all Strings whose length is greater than seven in addition to so nosotros are converting them to lowercase using the map() function.
Now, what create you lot think, how volition this plan execute? rate to bottom or bottom to top?
Many of you lot volition retrieve that later the commencement filter() execution you lot volition acquire a Stream containing ii elements "EURO/INR" in addition to "USD/EURO" in addition to peek() volition impress those ii elements.
Well, that's non the case, since Streams are executed lazily, zilch volition occur until the collect() method volition execute, which is the final method.
This is proved past times the next output from running inwards a higher house code into Eclipse IDE or ascendence prompt, it volition impress the next lines:
The cardinal signal to greenback hither is that values are filtered in addition to mapped i past times one, non together. It agency the code is executed backward when the collect() method calls the Collectors.toList() to acquire the number inwards a List, it asks map() business office which inwards plough inquire the filter() method.
Since filter() is lazy it returns the commencement chemical cistron whose length is greater than seven in addition to sits dorsum until map() inquire again.
You tin run across that peek() method clearly impress the value of current inwards the pipeline later each telephone call upwards to filter() method. You tin farther join
The peek() method await a Consumer functional interface to perform a non-interfering activity on the elements of this stream, ordinarily printing them using forEach() method.
Btw, the sole argue of using peek() is debugging Stream pipeline, fifty-fifty the API itself says that peek() is exclusively at that topographic point for debugging, but it does aid to empathize the lazy evaluation technique Stream uses to improve performance.
Lazy evaluation agency zilch is evaluated inwards the Stream until a final method similar forEach(), collect() or reduce() is called in addition to processing stops equally presently equally the number is obtained, which agency non all the elements of Stream is processed always.
It all depends upon what form of number you lot desire from Stream. For example, if you lot telephone call upwards findFirst() method so equally presently equally it finds the commencement chemical cistron fulling the criterion, processing stops. If you lot desire to empathize lazy evaluation inwards depth, you lot tin too cheque a comprehensive Java course of pedagogy like The Complete Java MasterClass or a specialized Java 8 books like Java 8 inwards Action by Manning publications.
Java 8 Stream peek() method Example
In social club to understand peek() method better, let's run across around code inwards action. How well-nigh using the filter and map methods in a chained pipeline?This is a really mutual code inwards Java 8 in addition to volition aid you lot to larn how current pipeline processing plant inwards Java 8? What happens inwards each step? What is the output or information inwards the current later each measuring etc?
Consider the next example, which calls the peek() method later each measuring inwards a Stream pipeline involving filter() and map() methods:
List<String> number = Stream.of("EURO/INR", "USD/AUD", "USD/GBP", "USD/EURO") .filter(e -> e.length() > 7) .peek(e -> System.out.println("Filtered value: " + e)) .map(String::toLowerCase) .peek(e -> System.out.println("Mapped value: " + e)) .collect(Collectors.toList());
In this example, nosotros create got a Stream of String in addition to so nosotros are filtering all Strings whose length is greater than seven in addition to so nosotros are converting them to lowercase using the map() function.
Now, what create you lot think, how volition this plan execute? rate to bottom or bottom to top?
Many of you lot volition retrieve that later the commencement filter() execution you lot volition acquire a Stream containing ii elements "EURO/INR" in addition to "USD/EURO" in addition to peek() volition impress those ii elements.
Well, that's non the case, since Streams are executed lazily, zilch volition occur until the collect() method volition execute, which is the final method.
This is proved past times the next output from running inwards a higher house code into Eclipse IDE or ascendence prompt, it volition impress the next lines:
Filtered value: EURO/INR Mapped value: euro/inr Filtered value: USD/EURO Mapped value: usd/euro
The cardinal signal to greenback hither is that values are filtered in addition to mapped i past times one, non together. It agency the code is executed backward when the collect() method calls the Collectors.toList() to acquire the number inwards a List, it asks map() business office which inwards plough inquire the filter() method.
Since filter() is lazy it returns the commencement chemical cistron whose length is greater than seven in addition to sits dorsum until map() inquire again.
You tin run across that peek() method clearly impress the value of current inwards the pipeline later each telephone call upwards to filter() method. You tin farther join
Important points
1) The peek() method of Stream storey is an intermediate method, thence you lot tin telephone call upwards other current methods later this.2) It returns a novel Stream, which is basically the current it got.
3) It accepts an object of functional interface Consumer to perform non-interfering activity e.g. printing values.
4) For parallel current pipelines, the activity may live on called at whatever fourth dimension in addition to whatever thread the chemical cistron is made available past times the upstream operation.
Btw, peek() is non the exclusively way to figure out what goes within a Stream pipeline, you lot tin too exercise your IDEs to create the heavy work. For example, If you lot are using IntelliIDEA from JetBrains, you lot tin too exercise their Java Stream Debugger Plugin to easily debug Java 8 code using map, filter in addition to collect inwards IDE itself, similar shown inwards next GIF diagram:
That's all well-nigh how to exercise the peek() method inwards Java 8. You tin exercise the peek() method for debugging. It allows you lot to run across the elements equally they catamenia past times a for sure signal inwards the pipeline. By using this you lot tin cheque whether your filter() method is working properly or not. You tin run across precisely which elements are got filtered past times using peek() inwards Java 8.
Further Learning
The Complete Java MasterClass
Java SE 8 for Really Impatient
courses)
P.S.: If you lot but desire to larn to a greater extent than well-nigh novel features inwards Java 8 so delight run across the course What's New inwards Java 8. It explains all the of import features of Java 8 e.g. lambda expressions, streams, functional interfaces, Optional, novel Date Time API in addition to other miscellaneous changes.
0 Response to "How To Debug Coffee Viii Stream Pipeline - Peek() Method Event Tutorial"
Post a Comment