Debug Java 8 Streams

Hello Everyone.

Here I am again, sharing info about yet another tool after the Lombok project which I find extremely useful and I’m sure any Java developer will fall in love with.

With the release of Java 8, Oracle introduced a new API – Stream. Even though the name could be confusing, this has nothing to do with InputStream and OutputStream from Java I/O. Streams are Monads, thus playing a big part in bringing functional programming to Java or for the simple minded like me a stream represents a sequence of elements and supports different kind of operations to perform computations upon those elements. Let’s look at this example:


List<String> words = new ArrayList<String>(Arrays.asList("Hello", "How are you?", "Welcome to Ivo's laboratory!",
"Mufasa", "Lion King", "Hello", "Hi"));

return words.stream()
.filter(text -> text.startsWith("H"))
.map(text -> text.substring(2))
.distinct()
.sorted()
.collect(Collectors.toList());

This code will take the list, filter all strings that start with ‘H’, then remove the first 2 letters, remove any duplicates, sort the elements and return the newly formed list. Awesome!

Where is the problem? – try to debug this. No matter if you write the code in 1,2,3,4 lines of code every time you step forward you will only see the object words with all it’s initial elements in the debugger. In this example it’s not a big issue but imagine you need to implement some super-duper-mega-trinity force-AI-planet saving comparator and you need to know if it’s doing it’s job properly. Well in worst case you have to create 12332443314 lists and assign the result from each stream computation into it. Fun, no?

WORRY NOT!!!

I have a solution for you. Well, technically it’s not me that came up with a solution but the people in Jet Brains did. They have provided us with a plugin called Java Stream Debugger for (of course) their IDE IntelliJ. Fortunately for everyone, it’s available for the free, community version of the environment.

How it works? – Easy…peasy.

First, install the plugin. File->Settings->Plugins->Browse Repositories->Type in the search bar Java Stream Debugger. Install, restart, GG.

Second, put a break point at the stream line. Start the program in Debug mode and once it stops you will see an extra button:

debugger_stream_button Press it, it won’t bite. You will get a beautiful window. On the left you have your original list, on the top are tabs which represent each computation that you are about to do on the stream and on the right you have the result from the step. All you have to do is press from tab to tab to see what is happening with your stream. Quite convenient!

stream_trace

Give it a try. Hit the Like button (or why not share) this post if it helped you solve another universally existential mystery in your code. 🙂 Also feel free to put a comment, sharing other ways that you have found to go around the streams.

Thank you and come again!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s