Optimize Java import declarations in IntelliJ IDEA

Run Optimize Imports

Select Code → Optimize Imports from the IntelliJ IDEA menu.

Before Optimize Imports: intellij1.png

After Optimize Imports: intellij2.png

(Confirmed with IntelliJ IDEA 2018.3.3 (Community Edition))

Content of optimization

The following optimizations are performed by executing Optimize Imports.

--Delete the import statement of the unused class --Sort the order of import declarations appropriately --Expand the on-demand import declaration (type-import-on-demand declaration) with the asterisk "\ *" to the individual import (single-type-import declaration) for each class.

Auto import - Help | IntelliJ IDEA

The Optimize Imports feature helps you remove unused imports, add missing imports, and organize import statements in the current file or in all files in a directory at once.

Optimize Imports shortcut keys

Optimize Imports can also be executed with shortcut keys. control + command + O on macOS Ctrl + Alt + O on Linux and Windows

IntelliJ IDEA DEFAULT KEYMAP

Type-Import-on-Demand Declarations settings

Type-Import-on-Demand Declarations is a Java syntax that uses an asterisk "*" to import all classes that belong to a package.

When using the Optimize Import function, if there are many import statements in the same package, they may be grouped together with an asterisk "\ *".

intellij3.png

For this, you can set the minimum number of import statements to be summarized with an asterisk "\ *" in "Class count to use import with'\ *'" of Settings Preferences-> Editor-> Code Style-> Java-> Imports.

intellij4.png

(Confirmed with IntelliJ IDEA 2018.3.3 (Community Edition))

Run Optimize imports automatically

By checking "Optimize imports on the fly (for current project)" in Settings Preferences-> Editor-> General-> Auto Import, Optimize imports will be executed automatically when the source code is modified.

スクリーンショット 2019-11-20 5.51.20.png

(Confirmed with IntelliJ IDEA 2019.2.4 (Community Edition))

Recommended Posts

Optimize Java import declarations in IntelliJ IDEA
Java + OpenCV 3.X in IntelliJ IDEA
Write Processing in IntelliJ IDEA
Import Excel data in Java
Do HelloWorld in Java / IntelliJ / Gradle
Create Java Spring Boot project in IntelliJ
About Stream Debugger in IntelliJ IDEA plugin
Java --Introduce CheckStyle plugin to IntelliJ IDEA and reflect it in formatter
What to do when you think you can't do Groovy-> Java in IntelliJ IDEA CE
Deploy Java application developed in IntelliJ IDEA environment to Alibaba Cloud ECS instance
About the idea of anonymous classes in Java
Partization in Java
Settings to display Japanese Javadoc in IntelliJ IDEA
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
IntelliJ IDEA settings
Import files of the same hierarchy in Java
FizzBuzz in Java
How to use java non-standard library on IntelliJ IDEA
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
[Java] A technique for writing constructors, getters, and setters in one shot with IntelliJ IDEA.
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Failed to launch checking at Kotlin Koans in IntelliJ IDEA
[Java] A story about IntelliJ IDEA teaching Map's putIfAbsent method