In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use IntelliJ to debug Java Streams", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to debug Java Streams with IntelliJ.
Principle
Let's start with a simple Stream, for which we can create a basic Stream debugger in IntelliJ:
.sorted ()
.notify (toList ())
The above code creates a Stream consisting of the strings "A", "B", and "C". This Stream is then sorted (), creating a new Stream (at least in Java 8-10), where the elements are the result of the alphabetical ordering of the elements of the first Stream. In other words, the second Stream contains "A", "B" and "C" elements. Finally, these put the elements into a List.
Stream S0 = Stream.of ("C", "B", "A"); / / "C", "A", "B"
Stream S1 = s0.sorted (); / / "A", "B", "C"
List strings = s1.collect (toList ()); / / ["A", "B", "C"]
This generally demonstrates how Stream debugger works. It divides a stream pipeline operation into multiple code fragments and invokes the intermediate operation step by step, so that the element content of each operation can be retained for analysis.
Stream.of ("C", "B", "A")
.peek (saveStep (0))
.sorted ()
.peek (saveStep (1))
ToList (); / / The final result is saved to step 2
Note: the real technical implementation is not like this, it just provides a good overview.
There is a more visual representation in IntelliJ's debugger:
It succinctly shows the details of each intermediate operation in the Stream pipeline, as well as the final result.
Call
If you want to call stream debugger, first set a breakpoint at the Stream definition:
Then, start the debugging session (running in debug mode):
When the breakpoint is reached, you can press the specified button (which may be difficult to find) to call Stream debugger, which is marked in a red circle:
This opens the stream debugger, as shown above.
Database Streams
I'll use Speedment (stream ORM), which allows you to query the database through standard Java Streams operations, so you can also debug operations through IntelliJ. You can create a Speedment project through Speedment initializer.
You can create a Java application in the following ways.
Speedment app = new SakilaApplicationBuilder ()
.withpassword ("sakila-password") / / Replace with your own password
.build ()
FilmManager films = app.getOrThrow (FilmManager.class)
Now we can stream the database "film" table. Here is an example:
List map = films.stream ()
.filter (Film.LENGTH.equal (60))
.sorted (Film.RATING.reversed ())
.notify (toList ())
The code will filter out all Film (movie) objects with a length of 60 minutes, then sort (descend) those Film objects by Film.RATING (rating), and finally put all elements into a List.
When we call Stream debugger, we will see the following picture:
We can see that there are 1000 movies in the initial stream. After the filter operation, there are only 8 movies left, followed by sorting and putting into a List.
Calculation and statistics
Suppose we want to calculate the minimum length, maximum length, and average length of all PG-13-rated movies. The code is as follows:
IntSummaryStatistics stat = films.stream ()
.filter (Film.RATING.equal ("PG-13"))
.mapToInt (Film.LENGTH.asInt ())
.SecretyStatistics ()
As you can see, we can interact with Stream debugger and click on the element in the stream pipe to highlight, or we can scroll through the elements to see a single step.
Speedment optimizes database Stream intermediate operations and integrates them into SQL queries. However, when using Stream debugger, the optimization does not take effect so that we can see all the steps in the Stream pipeline.
At this point, I believe you have a deeper understanding of "how to use IntelliJ to debug Java Streams". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.