IntelliJ IDEA 2016.3 EAP Helps Migrate to Java 8’s Stream APIs

Posted on by

In one of her recent articles, Trisha Gee wrote about refactoring to Java 8. Among many great tips about Java 8, the article also showed how IntelliJ IDEA helps write correct and idiomatic Java 8. With IntelliJ IDEA 2016.3, we’ve gone further and extended our existing inspections to support non-trivial cases.

Now, if you click Alt+Enter inside a for-loop incrementing a local variable defined above, the IDE will prompt you to replace it with a chain of the steam API calls ending with count(). Note, if the loops are nested, the IDE will use flatMap():

If the variable is incremented by a number returned from another method, the IDE will use the mapToInt()/mapToLong()/mapToDouble():

If, instead of incrementing a variable, the loop adds non-primitive objects to a collection, the IDE will replace it with a chain of calls ending with collect():

If the object being added to the collection is a result of another method, the IDE will handle it in an idiomatic way by using map():

How non-trivial the code supported by these inspections could be? Quite much. Consider a loop that heavily uses a continue statement:

Similar to continue, the IDE will correctly handle return statements too, using anyMatch():

If you decide to use these inspections, make sure to adjust the default Code Style settings to avoid exceeding the right margin for long method chains. Here, you have a choice, depending on your preferences.

Chop down chain method calls if they’re long (most preferable for Java 8)

2. Wrap method call arguments

At the moment we’re considering changing these defaults. If have ideas how to do it with no harm to existing users, let us know.

The inspections are enabled by default but their severity is set to the minimum–which means no warnings. This is yet another new feature of IntelliJ IDEA 2016.

You can change the severity of any inspection in the Settings:

If you want, you can apply these inspections in the batch mode on a scope of your choice:

We hope the new inspection will help you make your Java 8 code write even more idiomatic and take the most out of the stream API. Those unfamiliar yet with the stream API, make sure to read Oracle’s tutorials: part 1 and part 2.

The last but not least, we’re also working to improve the layout of VCS Log:

Commit details have moved to the right, giving more space to the Log itself.

Commit messages in the table are now aligned and thus more readable.

More changes are yet coming.

That’s it for today. To give the new features a try, download the EAP build. Don’t forget to write us back (using our issue tracker) about your impressions and bugs if any.

Develop with Pleasure!