I cause got been writing almost roughly of import methods from Java SE 8 like map(), flatMap(), collect() etc from quite roughly fourth dimension together with today I'll portion my sense almost roughly other useful method peek() from java.utill.stream.Stream class. The peek() method of Stream degree tin dismiss survive real useful to debug together with sympathise streams inwards Java 8. You tin dismiss usage the peek() method to run into the elements equally they catamenia from i measurement to roughly other similar when y'all usage the filter() method for filtering, y'all tin dismiss genuinely run into 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 together with performs the activity requested yesteryear the client.
The peek() method await a Consumer functional interface to perform a non-interfering activity on the elements of this stream, commonly 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 solely at that topographic point for debugging, but it does assistance to sympathise the lazy evaluation technique Stream uses to improve performance.
Lazy evaluation agency naught is evaluated inwards the Stream until a final method similar forEach(), collect() or reduce() is called together with processing stops equally presently equally the effect is obtained, which agency non all the elements of Stream is processed always.
It all depends upon what form of effect y'all desire from Stream. For example, if y'all telephone weep upwards findFirst() method together with so equally presently equally it finds the starting fourth dimension chemical ingredient fulling the criterion, processing stops. If y'all desire to sympathise lazy evaluation inwards depth, y'all tin dismiss besides 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 real mutual code inwards Java 8 together with volition assistance y'all to larn how current pipeline processing plant inwards Java 8? What happens inwards each step? What is the output or information inwards the current subsequently each measurement etc?
Consider the next example, which calls the peek() method subsequently each measurement inwards a Stream pipeline involving filter() and map() methods:
In this example, nosotros cause got a Stream of String together with and so nosotros are filtering all Strings whose length is greater than vii together with and so nosotros are converting them to lowercase using the map() function.
Now, what create y'all think, how volition this plan execute? transcend to bottom or bottom to top?
Many of y'all volition remember that subsequently the starting fourth dimension filter() execution y'all volition acquire a Stream containing 2 elements "EURO/INR" together with "USD/EURO" together with peek() volition impress those 2 elements.
Well, that's non the case, since Streams are executed lazily, naught volition tumble out until the collect() method volition execute, which is the final method.
This is proved yesteryear the next output from running higher upwards code into Eclipse IDE or ascendance prompt, it volition impress the next lines:
The telephone commutation betoken to greenback hither is that values are filtered together with mapped i yesteryear one, non together. It agency the code is executed backward when the collect() method calls the Collectors.toList() to acquire the effect inwards a List, it asks map() role which inwards plough enquire the filter() method.
Since filter() is lazy it returns the starting fourth dimension chemical ingredient whose length is greater than vii together with sits dorsum until map() enquire again.
You tin dismiss run into that peek() method clearly impress the value of current inwards the pipeline subsequently each telephone weep upwards to filter() method. You tin dismiss farther join
The peek() method await a Consumer functional interface to perform a non-interfering activity on the elements of this stream, commonly 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 solely at that topographic point for debugging, but it does assistance to sympathise the lazy evaluation technique Stream uses to improve performance.
Lazy evaluation agency naught is evaluated inwards the Stream until a final method similar forEach(), collect() or reduce() is called together with processing stops equally presently equally the effect is obtained, which agency non all the elements of Stream is processed always.
It all depends upon what form of effect y'all desire from Stream. For example, if y'all telephone weep upwards findFirst() method together with so equally presently equally it finds the starting fourth dimension chemical ingredient fulling the criterion, processing stops. If y'all desire to sympathise lazy evaluation inwards depth, y'all tin dismiss besides 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 gild to understand peek() method better, let's run into roughly code inwards action. How almost using the filter and map methods in a chained pipeline?This is a real mutual code inwards Java 8 together with volition assistance y'all to larn how current pipeline processing plant inwards Java 8? What happens inwards each step? What is the output or information inwards the current subsequently each measurement etc?
Consider the next example, which calls the peek() method subsequently each measurement inwards a Stream pipeline involving filter() and map() methods:
List<String> effect = 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 cause got a Stream of String together with and so nosotros are filtering all Strings whose length is greater than vii together with and so nosotros are converting them to lowercase using the map() function.
Now, what create y'all think, how volition this plan execute? transcend to bottom or bottom to top?
Many of y'all volition remember that subsequently the starting fourth dimension filter() execution y'all volition acquire a Stream containing 2 elements "EURO/INR" together with "USD/EURO" together with peek() volition impress those 2 elements.
Well, that's non the case, since Streams are executed lazily, naught volition tumble out until the collect() method volition execute, which is the final method.
This is proved yesteryear the next output from running higher upwards code into Eclipse IDE or ascendance prompt, it volition impress the next lines:
Filtered value: EURO/INR Mapped value: euro/inr Filtered value: USD/EURO Mapped value: usd/euro
The telephone commutation betoken to greenback hither is that values are filtered together with mapped i yesteryear one, non together. It agency the code is executed backward when the collect() method calls the Collectors.toList() to acquire the effect inwards a List, it asks map() role which inwards plough enquire the filter() method.
Since filter() is lazy it returns the starting fourth dimension chemical ingredient whose length is greater than vii together with sits dorsum until map() enquire again.
You tin dismiss run into that peek() method clearly impress the value of current inwards the pipeline subsequently each telephone weep upwards to filter() method. You tin dismiss farther join

Important points
1) The peek() method of Stream degree is an intermediate method, thus y'all tin dismiss telephone weep upwards other current methods subsequently 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 survive called at whatever fourth dimension together with whatever thread the chemical ingredient is made available yesteryear the upstream operation.
Btw, peek() is non the solely way to figure out what goes within a Stream pipeline, y'all tin dismiss besides usage your IDEs to create the heavy work. For example, If y'all are using IntelliIDEA from JetBrains, y'all tin dismiss besides usage their Java Stream Debugger Plugin to easily debug Java 8 code using map, filter together with collect inwards IDE itself, similar shown inwards next GIF diagram:
That's all almost how to usage the peek() method inwards Java 8. You tin dismiss usage the peek() method for debugging. It allows y'all to run into the elements equally they catamenia yesteryear a sure enough betoken inwards the pipeline. By using this y'all tin dismiss cheque whether your filter() method is working properly or not. You tin dismiss run into just which elements are got filtered yesteryear using peek() inwards Java 8.
Further Learning
The Complete Java MasterClass
Java SE 8 for Really Impatient
courses)
P.S.: If y'all merely desire to larn to a greater extent than almost novel features inwards Java 8 together with so delight run into 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 together with other miscellaneous changes.
No comments:
Post a Comment