Ctrl + Alt + O (Code → Optimize Imports...) is what you're looking for, both on Windows/Linux and macOS keymaps.
It says "Optimize", but, if configured to do so, it will also:
You can tune the auto-import settings under "Settings → Editor → General → Auto Import" as described by Dave.
You can also modify how the imports are auto-ordered under "Settings → Editor → Code Style → Java → Imports"
Under "Settings -> Editor -> General -> Auto Import"
there are several options regarding automatic imports. Only unambiguous imports may be added automatically; this is one of the options.
Simple & short solution worked for me.
Go to File
-> Settings
-> Editor
-> Auto Import
-> Java
(left panel) and make the below things:
Select check box for "Add unambigious imports on the fly
" and "Optimize imports on the fly
"
Refer this.
In addition to Optimize Imports
and Auto Import
, which were pointed out by @dave-newton and @ryan-stewart in earlier answers, go to:
File menu > Settings > Code Style > Java > Imports
File menu > Settings > Editor > Code Style > Java > Imports
(thanks to @mathias-bader for the hint!)
There you can fine tune the grouping and order or imports, "Class count to use import with '*'
", etc.
Note:
since IDEA 13 you can configure the project default settings from the IDEA "start page": Configure > Project defaults > Settings > ...
. Then every new project will have those default settings:
July 2015 - I have concluded that IntelliJ does not support automatically resolving imports with a single function. "Organize imports" simply removes unused imports, it does not resolve unimported types. Control-Space resolves a single unimported type. There does not exist a single action to resolve all types' imports.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With