Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

IntelliJ in the latest release has added some really helpful advanced refactoring that will convert to Java 8 Streams. This is a great feature and will really help people smartly and safely upgrade their legacy Java 6/7 code.

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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 and correct idiomatic Java 8 code. 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 could the code supported by these inspections be? Quite a lot. 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.

  1. 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 even more idiomatic and make the most out of the stream API. Those unfamiliar with the stream API, make sure to read Oracle’s tutorials: part 1 and part 2.

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 still 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.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,intellij ,ide ,stream ,java 8

Published at DZone with permission of Andrey Cheptcov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}