Select Code → Optimize Imports from the IntelliJ IDEA menu.
Before Optimize Imports:
After Optimize Imports:
(Confirmed with IntelliJ IDEA 2018.3.3 (Community Edition))
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 can also be executed with shortcut keys. control + command + O on macOS Ctrl + Alt + O on Linux and Windows
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 "\ *".
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.
(Confirmed with IntelliJ IDEA 2018.3.3 (Community Edition))
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.
(Confirmed with IntelliJ IDEA 2019.2.4 (Community Edition))
Recommended Posts