Home > Java > javaTutorial > How to debug Java Streams using IntelliJ

How to debug Java Streams using IntelliJ

王林
Release: 2023-04-26 22:01:06
forward
1520 people have browsed it

Principle

Let's start with a simple Stream for which we can create a basic Stream debugger in IntelliJ:

.sorted()

.collect(toList());

The above code creates a Stream consisting of the strings "A", "B", and "C". This Stream is then sorted()ed, creating a new Stream (at least in Java 8-10) whose elements are the alphabetical order of the elements of the first Stream. In other words, the second Stream contains three elements: "A", "B", and "C". 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 the Stream debugger works. It splits a stream pipeline operation into multiple code fragments and calls intermediate operations 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))

.collect(toList()); // The final result is saved to step 2

NOTE: The actual technical implementation is not like this, it just provides a good overview.

There is a more visual representation in IntelliJ’s debugger:

It concisely and clearly displays the details of every intermediate operation in the Stream pipeline, as well as the final result.

transfer

If you want to call the stream debugger, you must first set a breakpoint at the Stream definition:

Then, start a debugging session (run in debug mode):

When a breakpoint is reached, you can press the specified button (which may be difficult to find) to call the Stream debugger, which is marked with a red circle below:

This opens the stream debugger, as shown above.

Database Streams

I'll be using Speedment (stream ORM), which allows querying the database via standard Java Streams operations and therefore debugging operations via IntelliJ. A Speedment project can be created via the Speedment initializer.

A Java application can be created 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())

.collect(toList());

The code will filter out all Film objects with a length of 60 minutes, then sort the Film objects (in descending order) by Film.RATING, and finally put all the elements into a List.

When we call the Stream debugger, we will see the following picture:

We can see that there are 1000 movies in the initial stream. After the filtering operation, only 8 movies are left, which are then sorted and put into a List.

Calculate statistics

Suppose we want to calculate the minimum, maximum, and average duration of all movies rated PG-13 (a type of movie rating system). code show as below:

IntSummaryStatistics stat = films.stream()

.filter(Film.RATING.equal("PG-13"))

.mapToInt(Film.LENGTH.asInt())

.summaryStatistics();

As you can see, we can interact with the Stream debugger and click on elements in the stream pipeline to highlight them, and we can also scroll between elements to view individual steps.

Speedment optimizes database Stream intermediate operations and integrates them into SQL queries. But when using the Stream debugger, the optimization does not take effect so that we can see all the operation steps in the Stream pipeline.

The above is the detailed content of How to debug Java Streams using IntelliJ. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template