There was a plug-in called Stream Debugger of IntelliJ IDEA that visualizes the data flow of Stream API, so I tried using it.
environment
reference
Java Stream Debugger
Want an easy way to debug chains of Java 8’ Stream API calls? Try IntelliJ IDEA with http://ow.ly/jT9w30bEY9i
— IntelliJ IDEA @intellijidea May 12, 2017
Set a breakpoint on the stream you want to debug and run it in debug mode.
Click the "Trace Current Stream Chain" icon that has been added to the Debug panel.
The Stream Trace screen will appear and you can check the data flow. (It may take some time to display depending on the amount of data etc.) There are two display modes, "Flat Mode" and "Split Mode", which can be switched with the button at the bottom of the screen. The figure below is "Flat Mode".
Sample code
List<String> colors = Arrays.asList(
"pink", "red", "orange", "brown", "yellow", "green", "blue", "gray", "white", "black", "purple",
"red", "white", "brown", "pink", "white", "orange", "yellow", "blue", "gray"
);
colors.stream()
.peek(System.out::println)
.distinct()
.sorted(Comparator.comparing(String::length))
.map(String::toUpperCase)
.forEach(System.out::println);
As shown in this figure, you can check on the screen how the data is handled in the intermediate processing.
Open Plugins and click the [Browse Repositories ...] button at the bottom of the screen.
Enter "Stream Debugger" in the search field to narrow down the Plugin. Once you find the Stream Debugger, click the Install button to install it.
After installation, restart.
A Kotlin extension for Java Stream Debugger plugin.
There is also a Kotlin extension plugin for Stream Debugger. The usage is the same, set a breakpoint in the Sequence you want to debug, run it in debug mode, and click the "Trace Current Stream Chain" icon in the Debug panel.
listOf(5, 5, 2, 1, 6, 4, 3, 2, 4, 3).asSequence()
.filter { it % 2 == 0 }
.distinct()
.sorted()
.forEach { println(it) }
Recommended Posts