IntelliJ Organize 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.
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:
- organize existing imports
- remove unneeded imports
- add new required imports
- add unambiguous imports on the fly
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"
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.