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

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!

About Andrey Cheptsov

Part-time dreamer. JVM/JavaScript geek. IntelliJIDEA Marketing Manager at JetBrains.
This entry was posted in EAP Releases, New Features and tagged , , , , . Bookmark the permalink.

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

  1. Onno van Paridon says:

    Great news the the VCS log is getting some attention. Can you comment if ‘Cherry picking’ for SVN (https://youtrack.jetbrains.com/issue/IDEA-99578) is also on the list of coming changes?

  2. Paul says:

    I’m having issues with this upgrading – I press Update and Restart, the IDE closes down and nothing happens. It doesn’t upgrade. I’m on OSX. Any ideas?

  3. Al Sweetman says:

    Sounds great – I notice in the video at the top that there is a “not equals” symbol rather than the “standard” != … is this just a font choice or is there some other EAP magic going on? Likewise for the -> being displayed as a proper arrow?

  4. nafg says:

    Amazing stuff. Any chance this refactorings will be supported in Scala?

  5. Updater says:

    Yet we still can’t simply update to this version: IntelliJ shows only the “download” button instead of “download and upgrade” :(.

  6. Brandon Johnson says:

    I wonder if we might be able to get an automatic refactoring for building a map?

    private Map getPeople() {
    List people = peopleMapper.getPeople();
    Map idToName = new HashMap();
    for (Person person : people) {
    int id = person.getId();
    String name = person.getName();
    idToName.put(id, name);
    }
    return idToName;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *