I possess got been writing near only about of import methods from Java SE 8 e.g. map(), flatMap() etc from quite only about fourth dimension as well as today I'll portion my sense near only about other useful method peek() from java.utill.stream.Stream class. The peek() method of Stream course of written report tin terminate live a real useful to debug as well as empathize streams inwards Java 8. You tin terminate usage the peek() method to encounter the elements equally they catamenia from 1 mensuration to only about other e.g. when you lot usage the filter() method for filtering, you lot tin terminate truly encounter 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 as well as performs the activity requested past times the client. The peek() method human face a Consumer instance to perform a non-interfering activity on the elements of this stream, normally printing them using forEach() method.
Btw, the sole argue of using peek() is debugging fifty-fifty the API itself says that peek() is alone at that topographic point for debugging, but it does aid to empathize the lazy evaluation technique Stream uses to improve performance. You tin terminate likewise see Java SE 8 for Really Impatient past times Cay S. Horstmann to larn to a greater extent than near the internal working of Stream inwards Java 8.
In gild to empathize peek() method better, let's encounter only about code inwards action. How near using the filter() as well as map() inwards a chained pipeline? This volition aid you lot to larn how your Java 8 code involving Stream works.
Consider the next example:
In this example, nosotros possess got a Stream of String as well as so nosotros are filtering all Strings whose length is greater than vii as well as so nosotros are converting them to lowercase using the map() function. Now, how produce you lot recall this programme volition execute? Many of you lot volition recall that subsequently the starting fourth dimension filter() execution you lot volition acquire a Stream containing 2 elements "EURO/INR" as well as "USD/EURO" as well as peek() volition impress those 2 elements. Well, that's non the case, since Streams are executed lazily, aught volition give off until the collect() method volition execute, which is the lastly 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 next lines:
The substitution betoken to authorities annotation hither is that values are filtered as well as mapped 1 past times one, non together. It agency the code is executed backwards, when collect() method calls the Collectors.toList() to acquire the effect inwards a List, it inquire map() component subdivision which inwards plow inquire the filter() method. Since filter() is lazy it provide the starting fourth dimension chemical component whose length is greater than vii as well as sit down dorsum until map() inquire again.
You tin terminate encounter that peek() method clearly impress the value of current inwards the pipeline subsequently each telephone phone to filter() method. You tin terminate farther read Java 8 inwards Action to larn to a greater extent than near unlike types functioning amongst Stream e.g. intermediate as well as lastly operation.
By looking at this output, tin terminate you lot explicate how the code would possess got been executed? Well, it seems that when to collect() inquire the instant filter() method, it inquire starting fourth dimension filter() as well as you lot tin terminate encounter that starting fourth dimension Lollipop passed the starting fourth dimension filter but couldn't top the instant 1 because it doesn't start amongst alphabetic quality "H".
So, the instant filter() in 1 trial again inquire first() filter for an element, it provide Jelly Bean, Ice Cream Sandwich, as well as HoneyComb 1 past times one. Since HoneyComb made passed instant filter it is collected past times Collector as well as in 1 trial again the same procedure happens but aborted subsequently GingerBread because all elements inwards Stream is already processed.
This clearly explains the lazy execution require of Stream equally opposed to eager iterative implementation as well as peek() aid you lot to empathize this better. See Mastering Lambdas: Java Programming inwards a Multicore World by Maurice Naftalin to larn to a greater extent than near lazy evaluation as well as performance produce goodness of Stream inwards Java 8.
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 called at whatever fourth dimension as well as whatever thread the chemical component is made available past times the upstream operation.
That's all near how to usage the peek() method inwards Java 8. You tin terminate usage the peek() method for debugging. It allows you lot to encounter the elements equally they catamenia past times a for sure betoken inwards the pipeline. By using this you lot tin terminate banking corporation gibe whether your filter() method is working properly or not. You tin terminate encounter just which elements are got filtered past times using peek() inwards Java 8.
Other Java 8 tutorials for Java Programmers
Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-
Btw, the sole argue of using peek() is debugging fifty-fifty the API itself says that peek() is alone at that topographic point for debugging, but it does aid to empathize the lazy evaluation technique Stream uses to improve performance. You tin terminate likewise see Java SE 8 for Really Impatient past times Cay S. Horstmann to larn to a greater extent than near the internal working of Stream inwards Java 8.
In gild to empathize peek() method better, let's encounter only about code inwards action. How near using the filter() as well as map() inwards a chained pipeline? This volition aid you lot to larn how your Java 8 code involving Stream works.
Consider the next example:
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 possess got a Stream of String as well as so nosotros are filtering all Strings whose length is greater than vii as well as so nosotros are converting them to lowercase using the map() function. Now, how produce you lot recall this programme volition execute? Many of you lot volition recall that subsequently the starting fourth dimension filter() execution you lot volition acquire a Stream containing 2 elements "EURO/INR" as well as "USD/EURO" as well as peek() volition impress those 2 elements. Well, that's non the case, since Streams are executed lazily, aught volition give off until the collect() method volition execute, which is the lastly 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 next lines:
Filtered value: EURO/INR Mapped value: euro/inr Filtered value: USD/EURO Mapped value: usd/euro
The substitution betoken to authorities annotation hither is that values are filtered as well as mapped 1 past times one, non together. It agency the code is executed backwards, when collect() method calls the Collectors.toList() to acquire the effect inwards a List, it inquire map() component subdivision which inwards plow inquire the filter() method. Since filter() is lazy it provide the starting fourth dimension chemical component whose length is greater than vii as well as sit down dorsum until map() inquire again.
You tin terminate encounter that peek() method clearly impress the value of current inwards the pipeline subsequently each telephone phone to filter() method. You tin terminate farther read Java 8 inwards Action to larn to a greater extent than near unlike types functioning amongst Stream e.g. intermediate as well as lastly operation.
How to usage peek() method inwards Java 8
As I said, the Stream.peek() method is real useful for debugging as well as understating the current related code inwards Java. Here are a couplet of to a greater extent than illustration of using peek() to empathize how mass information operations is processed past times Stream utility.package test; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * Java 8 peek() method illustration */ public class Test { public static void main(String[] args) { List<String> versions = new ArrayList<>(); versions.add("Lollipop"); versions.add("KitKat"); versions.add("Jelly Bean"); versions.add("Ice Cream Sandwidch"); versions.add("Honeycomb"); versions.add("Gingerbread"); // filtering all vaersion which are longer than vii characters versions.stream() .filter(s -> s.length() > 7) .peek(e -> System.out.println("After the starting fourth dimension filter: " + e)) .filter(s -> s.startsWith("H")) .peek(e -> System.out.println("After the instant filter: " + e)) .collect(Collectors.toSet()); } } Output After the first filter: Lollipop After the first filter: Jelly Bean After the first filter: Ice Cream Sandwich After the first filter: Honeycomb After the instant filter: Honeycomb After the first filter: Gingerbread
By looking at this output, tin terminate you lot explicate how the code would possess got been executed? Well, it seems that when to collect() inquire the instant filter() method, it inquire starting fourth dimension filter() as well as you lot tin terminate encounter that starting fourth dimension Lollipop passed the starting fourth dimension filter but couldn't top the instant 1 because it doesn't start amongst alphabetic quality "H".
So, the instant filter() in 1 trial again inquire first() filter for an element, it provide Jelly Bean, Ice Cream Sandwich, as well as HoneyComb 1 past times one. Since HoneyComb made passed instant filter it is collected past times Collector as well as in 1 trial again the same procedure happens but aborted subsequently GingerBread because all elements inwards Stream is already processed.
This clearly explains the lazy execution require of Stream equally opposed to eager iterative implementation as well as peek() aid you lot to empathize this better. See Mastering Lambdas: Java Programming inwards a Multicore World by Maurice Naftalin to larn to a greater extent than near lazy evaluation as well as performance produce goodness of Stream inwards Java 8.
Important points
1) The peek() method of Stream course of written report is an intermediate method, therefore you lot tin terminate telephone phone 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 live called at whatever fourth dimension as well as whatever thread the chemical component is made available past times the upstream operation.
That's all near how to usage the peek() method inwards Java 8. You tin terminate usage the peek() method for debugging. It allows you lot to encounter the elements equally they catamenia past times a for sure betoken inwards the pipeline. By using this you lot tin terminate banking corporation gibe whether your filter() method is working properly or not. You tin terminate encounter just which elements are got filtered past times using peek() inwards Java 8.
Other Java 8 tutorials for Java Programmers
- Java 8 - String.join() example
- Java 8 - lambda examples
- Java 8 - comparator example
- Java 8 - filter() example
- Java 8 - engagement as well as fourth dimension examples
- Java 8 - current examples
- Java 8 - forEach() example
- Java 8 - books
- Java 8 - listing to map examples
Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-


0 Response to "Java eight Stream.peek() Example to debug Stream"
Posting Komentar